Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / socket-objects.html
CommitLineData
86530b38
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="next" href="ssl-objects.html" />
13<link rel="prev" href="module-socket.html" />
14<link rel="parent" href="module-socket.html" />
15<link rel="next" href="ssl-objects.html" />
16<meta name='aesop' content='information' />
17<title>7.2.1 Socket Objects </title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="7.2 socket "
25 href="module-socket.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="7.2 socket "
28 href="module-socket.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="7.2.2 SSL Objects"
31 href="ssl-objects.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="module-socket.html">7.2 socket </A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="module-socket.html">7.2 socket </A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="ssl-objects.html">7.2.2 SSL Objects</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION009210000000000000000"></A><A NAME="socket-objects"></A>
56<BR>
577.2.1 Socket Objects
58</H2>
59
60<P>
61Socket objects have the following methods. Except for
62<tt class="method">makefile()</tt> these correspond to <span class="Unix">Unix</span> system calls
63applicable to sockets.
64
65<P>
66<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
67 <td><nobr><b><tt id='l2h-2650' xml:id='l2h-2650' class="method">accept</tt></b>(</nobr></td>
68 <td><var></var>)</td></tr></table></dt>
69<dd>
70Accept a connection.
71The socket must be bound to an address and listening for connections.
72The return value is a pair <code>(<var>conn</var>, <var>address</var>)</code>
73where <var>conn</var> is a <em>new</em> socket object usable to send and
74receive data on the connection, and <var>address</var> is the address bound
75to the socket on the other end of the connection.
76</dl>
77
78<P>
79<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
80 <td><nobr><b><tt id='l2h-2651' xml:id='l2h-2651' class="method">bind</tt></b>(</nobr></td>
81 <td><var>address</var>)</td></tr></table></dt>
82<dd>
83Bind the socket to <var>address</var>. The socket must not already be bound.
84(The format of <var>address</var> depends on the address family -- see
85above.) <span class="note"><b class="label">Note:</b>
86This method has historically accepted a pair
87of parameters for <tt class="constant">AF_INET</tt> addresses instead of only a
88tuple. This was never intentional and is no longer available in
89Python 2.0 and later.</span>
90</dl>
91
92<P>
93<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
94 <td><nobr><b><tt id='l2h-2652' xml:id='l2h-2652' class="method">close</tt></b>(</nobr></td>
95 <td><var></var>)</td></tr></table></dt>
96<dd>
97Close the socket. All future operations on the socket object will fail.
98The remote end will receive no more data (after queued data is flushed).
99Sockets are automatically closed when they are garbage-collected.
100</dl>
101
102<P>
103<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
104 <td><nobr><b><tt id='l2h-2653' xml:id='l2h-2653' class="method">connect</tt></b>(</nobr></td>
105 <td><var>address</var>)</td></tr></table></dt>
106<dd>
107Connect to a remote socket at <var>address</var>.
108(The format of <var>address</var> depends on the address family -- see
109above.) <span class="note"><b class="label">Note:</b>
110This method has historically accepted a pair
111of parameters for <tt class="constant">AF_INET</tt> addresses instead of only a
112tuple. This was never intentional and is no longer available in
113Python 2.0 and later.</span>
114</dl>
115
116<P>
117<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
118 <td><nobr><b><tt id='l2h-2654' xml:id='l2h-2654' class="method">connect_ex</tt></b>(</nobr></td>
119 <td><var>address</var>)</td></tr></table></dt>
120<dd>
121Like <code>connect(<var>address</var>)</code>, but return an error indicator
122instead of raising an exception for errors returned by the C-level
123<tt class="cfunction">connect()</tt> call (other problems, such as ``host not found,''
124can still raise exceptions). The error indicator is <code>0</code> if the
125operation succeeded, otherwise the value of the <tt class="cdata">errno</tt>
126variable. This is useful to support, for example, asynchronous connects.
127<span class="note"><b class="label">Note:</b>
128This method has historically accepted a pair of
129parameters for <tt class="constant">AF_INET</tt> addresses instead of only a tuple.
130This was never intentional and is no longer available in Python
1312.0 and later.</span>
132</dl>
133
134<P>
135<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
136 <td><nobr><b><tt id='l2h-2655' xml:id='l2h-2655' class="method">fileno</tt></b>(</nobr></td>
137 <td><var></var>)</td></tr></table></dt>
138<dd>
139Return the socket's file descriptor (a small integer). This is useful
140with <tt class="function">select.select()</tt>.
141
142<P>
143Under Windows the small integer returned by this method cannot be used where
144a file descriptor can be used (such as <tt class="function">os.fdopen()</tt>). <span class="Unix">Unix</span> does
145not have this limitation.
146</dl>
147
148<P>
149<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
150 <td><nobr><b><tt id='l2h-2656' xml:id='l2h-2656' class="method">getpeername</tt></b>(</nobr></td>
151 <td><var></var>)</td></tr></table></dt>
152<dd>
153Return the remote address to which the socket is connected. This is
154useful to find out the port number of a remote IPv4/v6 socket, for instance.
155(The format of the address returned depends on the address family --
156see above.) On some systems this function is not supported.
157</dl>
158
159<P>
160<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
161 <td><nobr><b><tt id='l2h-2657' xml:id='l2h-2657' class="method">getsockname</tt></b>(</nobr></td>
162 <td><var></var>)</td></tr></table></dt>
163<dd>
164Return the socket's own address. This is useful to find out the port
165number of an IPv4/v6 socket, for instance.
166(The format of the address returned depends on the address family --
167see above.)
168</dl>
169
170<P>
171<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
172 <td><nobr><b><tt id='l2h-2658' xml:id='l2h-2658' class="method">getsockopt</tt></b>(</nobr></td>
173 <td><var>level, optname</var><big>[</big><var>, buflen</var><big>]</big><var></var>)</td></tr></table></dt>
174<dd>
175Return the value of the given socket option (see the <span class="Unix">Unix</span> man page
176<span class="manpage"><i>getsockopt</i>(2)</span>). The needed symbolic constants
177(<tt class="constant">SO_*</tt> etc.) are defined in this module. If <var>buflen</var>
178is absent, an integer option is assumed and its integer value
179is returned by the function. If <var>buflen</var> is present, it specifies
180the maximum length of the buffer used to receive the option in, and
181this buffer is returned as a string. It is up to the caller to decode
182the contents of the buffer (see the optional built-in module
183<tt class="module"><a href="module-struct.html">struct</a></tt> for a way to decode C structures encoded as strings).
184</dl>
185
186<P>
187<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
188 <td><nobr><b><tt id='l2h-2659' xml:id='l2h-2659' class="method">listen</tt></b>(</nobr></td>
189 <td><var>backlog</var>)</td></tr></table></dt>
190<dd>
191Listen for connections made to the socket. The <var>backlog</var> argument
192specifies the maximum number of queued connections and should be at
193least 1; the maximum value is system-dependent (usually 5).
194</dl>
195
196<P>
197<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
198 <td><nobr><b><tt id='l2h-2660' xml:id='l2h-2660' class="method">makefile</tt></b>(</nobr></td>
199 <td><var></var><big>[</big><var>mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
200<dd>
201Return a <i class="dfn">file object</i> associated with the socket. (File objects
202are described in <A href="bltin-file-objects.html#bltin-file-objects">2.3.9</A>, ``File Objects.'')
203The file object references a <tt class="cfunction">dup()</tt>ped version of the
204socket file descriptor, so the file object and socket object may be
205closed or garbage-collected independently.
206The socket should be in blocking mode.
207<a id='l2h-2671' xml:id='l2h-2671'></a>The optional <var>mode</var>
208and <var>bufsize</var> arguments are interpreted the same way as by the
209built-in <tt class="function">file()</tt> function; see ``Built-in Functions''
210(section <A href="built-in-funcs.html#built-in-funcs">2.1</A>) for more information.
211</dl>
212
213<P>
214<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
215 <td><nobr><b><tt id='l2h-2661' xml:id='l2h-2661' class="method">recv</tt></b>(</nobr></td>
216 <td><var>bufsize</var><big>[</big><var>, flags</var><big>]</big><var></var>)</td></tr></table></dt>
217<dd>
218Receive data from the socket. The return value is a string representing
219the data received. The maximum amount of data to be received
220at once is specified by <var>bufsize</var>. See the <span class="Unix">Unix</span> manual page
221<span class="manpage"><i>recv</i>(2)</span> for the meaning of the optional argument
222<var>flags</var>; it defaults to zero.
223</dl>
224
225<P>
226<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
227 <td><nobr><b><tt id='l2h-2662' xml:id='l2h-2662' class="method">recvfrom</tt></b>(</nobr></td>
228 <td><var>bufsize</var><big>[</big><var>, flags</var><big>]</big><var></var>)</td></tr></table></dt>
229<dd>
230Receive data from the socket. The return value is a pair
231<code>(<var>string</var>, <var>address</var>)</code> where <var>string</var> is a string
232representing the data received and <var>address</var> is the address of the
233socket sending the data. The optional <var>flags</var> argument has the
234same meaning as for <tt class="method">recv()</tt> above.
235(The format of <var>address</var> depends on the address family -- see above.)
236</dl>
237
238<P>
239<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
240 <td><nobr><b><tt id='l2h-2663' xml:id='l2h-2663' class="method">send</tt></b>(</nobr></td>
241 <td><var>string</var><big>[</big><var>, flags</var><big>]</big><var></var>)</td></tr></table></dt>
242<dd>
243Send data to the socket. The socket must be connected to a remote
244socket. The optional <var>flags</var> argument has the same meaning as for
245<tt class="method">recv()</tt> above. Returns the number of bytes sent.
246Applications are responsible for checking that all data has been sent;
247if only some of the data was transmitted, the application needs to
248attempt delivery of the remaining data.
249</dl>
250
251<P>
252<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
253 <td><nobr><b><tt id='l2h-2664' xml:id='l2h-2664' class="method">sendall</tt></b>(</nobr></td>
254 <td><var>string</var><big>[</big><var>, flags</var><big>]</big><var></var>)</td></tr></table></dt>
255<dd>
256Send data to the socket. The socket must be connected to a remote
257socket. The optional <var>flags</var> argument has the same meaning as for
258<tt class="method">recv()</tt> above. Unlike <tt class="method">send()</tt>, this method continues
259to send data from <var>string</var> until either all data has been sent or
260an error occurs. <code>None</code> is returned on success. On error, an
261exception is raised, and there is no way to determine how much data,
262if any, was successfully sent.
263</dl>
264
265<P>
266<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
267 <td><nobr><b><tt id='l2h-2665' xml:id='l2h-2665' class="method">sendto</tt></b>(</nobr></td>
268 <td><var>string</var><big>[</big><var>, flags</var><big>]</big><var>, address</var>)</td></tr></table></dt>
269<dd>
270Send data to the socket. The socket should not be connected to a
271remote socket, since the destination socket is specified by
272<var>address</var>. The optional <var>flags</var> argument has the same
273meaning as for <tt class="method">recv()</tt> above. Return the number of bytes sent.
274(The format of <var>address</var> depends on the address family -- see above.)
275</dl>
276
277<P>
278<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
279 <td><nobr><b><tt id='l2h-2666' xml:id='l2h-2666' class="method">setblocking</tt></b>(</nobr></td>
280 <td><var>flag</var>)</td></tr></table></dt>
281<dd>
282Set blocking or non-blocking mode of the socket: if <var>flag</var> is 0,
283the socket is set to non-blocking, else to blocking mode. Initially
284all sockets are in blocking mode. In non-blocking mode, if a
285<tt class="method">recv()</tt> call doesn't find any data, or if a
286<tt class="method">send()</tt> call can't immediately dispose of the data, a
287<tt class="exception">error</tt> exception is raised; in blocking mode, the calls
288block until they can proceed.
289<code>s.setblocking(0)</code> is equivalent to <code>s.settimeout(0)</code>;
290<code>s.setblocking(1)</code> is equivalent to <code>s.settimeout(None)</code>.
291</dl>
292
293<P>
294<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
295 <td><nobr><b><tt id='l2h-2667' xml:id='l2h-2667' class="method">settimeout</tt></b>(</nobr></td>
296 <td><var>value</var>)</td></tr></table></dt>
297<dd>
298Set a timeout on blocking socket operations. The <var>value</var> argument
299can be a nonnegative float expressing seconds, or <code>None</code>.
300If a float is
301given, subsequent socket operations will raise an <tt class="exception">timeout</tt>
302exception if the timeout period <var>value</var> has elapsed before the
303operation has completed. Setting a timeout of <code>None</code> disables
304timeouts on socket operations.
305<code>s.settimeout(0.0)</code> is equivalent to <code>s.setblocking(0)</code>;
306<code>s.settimeout(None)</code> is equivalent to <code>s.setblocking(1)</code>.
307
308<span class="versionnote">New in version 2.3.</span>
309
310</dl>
311
312<P>
313<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
314 <td><nobr><b><tt id='l2h-2668' xml:id='l2h-2668' class="method">gettimeout</tt></b>(</nobr></td>
315 <td><var></var>)</td></tr></table></dt>
316<dd>
317Returns the timeout in floating seconds associated with socket
318operations, or <code>None</code> if no timeout is set. This reflects
319the last call to <tt class="method">setblocking()</tt> or <tt class="method">settimeout()</tt>.
320
321<span class="versionnote">New in version 2.3.</span>
322
323</dl>
324
325<P>
326Some notes on socket blocking and timeouts: A socket object can be in
327one of three modes: blocking, non-blocking, or timeout. Sockets are
328always created in blocking mode. In blocking mode, operations block
329until complete. In non-blocking mode, operations fail (with an error
330that is unfortunately system-dependent) if they cannot be completed
331immediately. In timeout mode, operations fail if they cannot be
332completed within the timeout specified for the socket. The
333<tt class="method">setblocking()</tt> method is simply a shorthand for certain
334<tt class="method">settimeout()</tt> calls.
335
336<P>
337Timeout mode internally sets the socket in non-blocking mode. The
338blocking and timeout modes are shared between file descriptors and
339socket objects that refer to the same network endpoint. A consequence
340of this is that file objects returned by the <tt class="method">makefile()</tt>
341method should only be used when the socket is in blocking mode; in
342timeout or non-blocking mode file operations that cannot be completed
343immediately will fail.
344
345<P>
346Note that the <tt class="method">connect()</tt> operation is subject to the timeout
347setting, and in general it is recommended to call
348<tt class="method">settimeout()</tt> before calling <tt class="method">connect()</tt>.
349
350<P>
351<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
352 <td><nobr><b><tt id='l2h-2669' xml:id='l2h-2669' class="method">setsockopt</tt></b>(</nobr></td>
353 <td><var>level, optname, value</var>)</td></tr></table></dt>
354<dd>
355Set the value of the given socket option (see the <span class="Unix">Unix</span> manual page
356<span class="manpage"><i>setsockopt</i>(2)</span>). The needed symbolic constants are defined in
357the <tt class="module">socket</tt> module (<tt class="constant">SO_*</tt> etc.). The value can be an
358integer or a string representing a buffer. In the latter case it is
359up to the caller to ensure that the string contains the proper bits
360(see the optional built-in module
361<tt class="module"><a href="module-struct.html">struct</a></tt><a id='l2h-2672' xml:id='l2h-2672'></a> for a way to encode C
362structures as strings).
363</dl>
364
365<P>
366<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
367 <td><nobr><b><tt id='l2h-2670' xml:id='l2h-2670' class="method">shutdown</tt></b>(</nobr></td>
368 <td><var>how</var>)</td></tr></table></dt>
369<dd>
370Shut down one or both halves of the connection. If <var>how</var> is
371<tt class="constant">SHUT_RD</tt>, further receives are disallowed. If <var>how</var> is <tt class="constant">SHUT_WR</tt>,
372further sends are disallowed. If <var>how</var> is <tt class="constant">SHUT_RDWR</tt>, further sends
373and receives are disallowed.
374</dl>
375
376<P>
377Note that there are no methods <tt class="method">read()</tt> or <tt class="method">write()</tt>;
378use <tt class="method">recv()</tt> and <tt class="method">send()</tt> without <var>flags</var> argument
379instead.
380
381<P>
382
383<DIV CLASS="navigation">
384<div class='online-navigation'>
385<p></p><hr />
386<table align="center" width="100%" cellpadding="0" cellspacing="2">
387<tr>
388<td class='online-navigation'><a rel="prev" title="7.2 socket "
389 href="module-socket.html"><img src='../icons/previous.png'
390 border='0' height='32' alt='Previous Page' width='32' /></A></td>
391<td class='online-navigation'><a rel="parent" title="7.2 socket "
392 href="module-socket.html"><img src='../icons/up.png'
393 border='0' height='32' alt='Up One Level' width='32' /></A></td>
394<td class='online-navigation'><a rel="next" title="7.2.2 SSL Objects"
395 href="ssl-objects.html"><img src='../icons/next.png'
396 border='0' height='32' alt='Next Page' width='32' /></A></td>
397<td align="center" width="100%">Python Library Reference</td>
398<td class='online-navigation'><a rel="contents" title="Table of Contents"
399 href="contents.html"><img src='../icons/contents.png'
400 border='0' height='32' alt='Contents' width='32' /></A></td>
401<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
402 border='0' height='32' alt='Module Index' width='32' /></a></td>
403<td class='online-navigation'><a rel="index" title="Index"
404 href="genindex.html"><img src='../icons/index.png'
405 border='0' height='32' alt='Index' width='32' /></A></td>
406</tr></table>
407<div class='online-navigation'>
408<b class="navlabel">Previous:</b>
409<a class="sectref" rel="prev" href="module-socket.html">7.2 socket </A>
410<b class="navlabel">Up:</b>
411<a class="sectref" rel="parent" href="module-socket.html">7.2 socket </A>
412<b class="navlabel">Next:</b>
413<a class="sectref" rel="next" href="ssl-objects.html">7.2.2 SSL Objects</A>
414</div>
415</div>
416<hr />
417<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
418</DIV>
419<!--End of Navigation Panel-->
420<ADDRESS>
421See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
422</ADDRESS>
423</BODY>
424</HTML>