Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="ssl-objects.html" /> | |
13 | <link rel="parent" href="module-socket.html" /> | |
14 | <link rel="next" href="module-select.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>7.2.3 Example </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="7.2.2 SSL Objects" | |
24 | href="ssl-objects.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="7.2 socket " | |
27 | href="module-socket.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="7.3 select " | |
30 | href="module-select.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="ssl-objects.html">7.2.2 SSL Objects</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-socket.html">7.2 socket </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-select.html">7.3 select </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION009230000000000000000"></A><A NAME="socket-example"></A> | |
55 | <BR> | |
56 | 7.2.3 Example | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | Here are four minimal example programs using the TCP/IP protocol: a | |
61 | server that echoes all data that it receives back (servicing only one | |
62 | client), and a client using it. Note that a server must perform the | |
63 | sequence <tt class="function">socket()</tt>, <tt class="method">bind()</tt>, <tt class="method">listen()</tt>, | |
64 | <tt class="method">accept()</tt> (possibly repeating the <tt class="method">accept()</tt> to service | |
65 | more than one client), while a client only needs the sequence | |
66 | <tt class="function">socket()</tt>, <tt class="method">connect()</tt>. Also note that the server | |
67 | does not <tt class="method">send()</tt>/<tt class="method">recv()</tt> on the | |
68 | socket it is listening on but on the new socket returned by | |
69 | <tt class="method">accept()</tt>. | |
70 | ||
71 | <P> | |
72 | The first two examples support IPv4 only. | |
73 | ||
74 | <P> | |
75 | <div class="verbatim"><pre> | |
76 | # Echo server program | |
77 | import socket | |
78 | ||
79 | HOST = '' # Symbolic name meaning the local host | |
80 | PORT = 50007 # Arbitrary non-privileged port | |
81 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
82 | s.bind((HOST, PORT)) | |
83 | s.listen(1) | |
84 | conn, addr = s.accept() | |
85 | print 'Connected by', addr | |
86 | while 1: | |
87 | data = conn.recv(1024) | |
88 | if not data: break | |
89 | conn.send(data) | |
90 | conn.close() | |
91 | </pre></div> | |
92 | ||
93 | <P> | |
94 | <div class="verbatim"><pre> | |
95 | # Echo client program | |
96 | import socket | |
97 | ||
98 | HOST = 'daring.cwi.nl' # The remote host | |
99 | PORT = 50007 # The same port as used by the server | |
100 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
101 | s.connect((HOST, PORT)) | |
102 | s.send('Hello, world') | |
103 | data = s.recv(1024) | |
104 | s.close() | |
105 | print 'Received', repr(data) | |
106 | </pre></div> | |
107 | ||
108 | <P> | |
109 | The next two examples are identical to the above two, but support both | |
110 | IPv4 and IPv6. | |
111 | The server side will listen to the first address family available | |
112 | (it should listen to both instead). | |
113 | On most of IPv6-ready systems, IPv6 will take precedence | |
114 | and the server may not accept IPv4 traffic. | |
115 | The client side will try to connect to the all addresses returned as a result | |
116 | of the name resolution, and sends traffic to the first one connected | |
117 | successfully. | |
118 | ||
119 | <P> | |
120 | <div class="verbatim"><pre> | |
121 | # Echo server program | |
122 | import socket | |
123 | import sys | |
124 | ||
125 | HOST = '' # Symbolic name meaning the local host | |
126 | PORT = 50007 # Arbitrary non-privileged port | |
127 | s = None | |
128 | for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): | |
129 | af, socktype, proto, canonname, sa = res | |
130 | try: | |
131 | s = socket.socket(af, socktype, proto) | |
132 | except socket.error, msg: | |
133 | s = None | |
134 | continue | |
135 | try: | |
136 | s.bind(sa) | |
137 | s.listen(1) | |
138 | except socket.error, msg: | |
139 | s.close() | |
140 | s = None | |
141 | continue | |
142 | break | |
143 | if s is None: | |
144 | print 'could not open socket' | |
145 | sys.exit(1) | |
146 | conn, addr = s.accept() | |
147 | print 'Connected by', addr | |
148 | while 1: | |
149 | data = conn.recv(1024) | |
150 | if not data: break | |
151 | conn.send(data) | |
152 | conn.close() | |
153 | </pre></div> | |
154 | ||
155 | <P> | |
156 | <div class="verbatim"><pre> | |
157 | # Echo client program | |
158 | import socket | |
159 | import sys | |
160 | ||
161 | HOST = 'daring.cwi.nl' # The remote host | |
162 | PORT = 50007 # The same port as used by the server | |
163 | s = None | |
164 | for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM): | |
165 | af, socktype, proto, canonname, sa = res | |
166 | try: | |
167 | s = socket.socket(af, socktype, proto) | |
168 | except socket.error, msg: | |
169 | s = None | |
170 | continue | |
171 | try: | |
172 | s.connect(sa) | |
173 | except socket.error, msg: | |
174 | s.close() | |
175 | s = None | |
176 | continue | |
177 | break | |
178 | if s is None: | |
179 | print 'could not open socket' | |
180 | sys.exit(1) | |
181 | s.send('Hello, world') | |
182 | data = s.recv(1024) | |
183 | s.close() | |
184 | print 'Received', repr(data) | |
185 | </pre></div> | |
186 | ||
187 | <DIV CLASS="navigation"> | |
188 | <div class='online-navigation'> | |
189 | <p></p><hr /> | |
190 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
191 | <tr> | |
192 | <td class='online-navigation'><a rel="prev" title="7.2.2 SSL Objects" | |
193 | href="ssl-objects.html"><img src='../icons/previous.png' | |
194 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
195 | <td class='online-navigation'><a rel="parent" title="7.2 socket " | |
196 | href="module-socket.html"><img src='../icons/up.png' | |
197 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
198 | <td class='online-navigation'><a rel="next" title="7.3 select " | |
199 | href="module-select.html"><img src='../icons/next.png' | |
200 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
201 | <td align="center" width="100%">Python Library Reference</td> | |
202 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
203 | href="contents.html"><img src='../icons/contents.png' | |
204 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
205 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
206 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
207 | <td class='online-navigation'><a rel="index" title="Index" | |
208 | href="genindex.html"><img src='../icons/index.png' | |
209 | border='0' height='32' alt='Index' width='32' /></A></td> | |
210 | </tr></table> | |
211 | <div class='online-navigation'> | |
212 | <b class="navlabel">Previous:</b> | |
213 | <a class="sectref" rel="prev" href="ssl-objects.html">7.2.2 SSL Objects</A> | |
214 | <b class="navlabel">Up:</b> | |
215 | <a class="sectref" rel="parent" href="module-socket.html">7.2 socket </A> | |
216 | <b class="navlabel">Next:</b> | |
217 | <a class="sectref" rel="next" href="module-select.html">7.3 select </A> | |
218 | </div> | |
219 | </div> | |
220 | <hr /> | |
221 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
222 | </DIV> | |
223 | <!--End of Navigation Panel--> | |
224 | <ADDRESS> | |
225 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
226 | </ADDRESS> | |
227 | </BODY> | |
228 | </HTML> |