Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / socket-example.html
CommitLineData
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>
567.2.3 Example
57</H2>
58
59<P>
60Here are four minimal example programs using the TCP/IP protocol: a
61server that echoes all data that it receives back (servicing only one
62client), and a client using it. Note that a server must perform the
63sequence <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
65more 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
67does not <tt class="method">send()</tt>/<tt class="method">recv()</tt> on the
68socket it is listening on but on the new socket returned by
69<tt class="method">accept()</tt>.
70
71<P>
72The first two examples support IPv4 only.
73
74<P>
75<div class="verbatim"><pre>
76# Echo server program
77import socket
78
79HOST = '' # Symbolic name meaning the local host
80PORT = 50007 # Arbitrary non-privileged port
81s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
82s.bind((HOST, PORT))
83s.listen(1)
84conn, addr = s.accept()
85print 'Connected by', addr
86while 1:
87 data = conn.recv(1024)
88 if not data: break
89 conn.send(data)
90conn.close()
91</pre></div>
92
93<P>
94<div class="verbatim"><pre>
95# Echo client program
96import socket
97
98HOST = 'daring.cwi.nl' # The remote host
99PORT = 50007 # The same port as used by the server
100s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
101s.connect((HOST, PORT))
102s.send('Hello, world')
103data = s.recv(1024)
104s.close()
105print 'Received', repr(data)
106</pre></div>
107
108<P>
109The next two examples are identical to the above two, but support both
110IPv4 and IPv6.
111The server side will listen to the first address family available
112(it should listen to both instead).
113On most of IPv6-ready systems, IPv6 will take precedence
114and the server may not accept IPv4 traffic.
115The client side will try to connect to the all addresses returned as a result
116of the name resolution, and sends traffic to the first one connected
117successfully.
118
119<P>
120<div class="verbatim"><pre>
121# Echo server program
122import socket
123import sys
124
125HOST = '' # Symbolic name meaning the local host
126PORT = 50007 # Arbitrary non-privileged port
127s = None
128for 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
143if s is None:
144 print 'could not open socket'
145 sys.exit(1)
146conn, addr = s.accept()
147print 'Connected by', addr
148while 1:
149 data = conn.recv(1024)
150 if not data: break
151 conn.send(data)
152conn.close()
153</pre></div>
154
155<P>
156<div class="verbatim"><pre>
157# Echo client program
158import socket
159import sys
160
161HOST = 'daring.cwi.nl' # The remote host
162PORT = 50007 # The same port as used by the server
163s = None
164for 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
178if s is None:
179 print 'could not open socket'
180 sys.exit(1)
181s.send('Hello, world')
182data = s.recv(1024)
183s.close()
184print '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>
225See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
226</ADDRESS>
227</BODY>
228</HTML>