home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Socket Objects -- Python library reference</TITLE>
- Next: <A HREF="../s/socket_example" TYPE="Next">Socket Example</A>
- Prev: <A HREF="../s/socket" TYPE="Prev">socket</A>
- Up: <A HREF="../s/socket" TYPE="Up">socket</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H2>7.2.1. Socket Objects</H2>
- Socket objects have the following methods. Except for
- <CODE>makefile()</CODE> these correspond to UNIX system calls applicable to
- sockets.
- <P>
- <DL><DT><B>accept</B> () -- Method on socket<DD>
- Accept a connection.
- The socket must be bound to an address and listening for connections.
- The return value is a pair <CODE>(<VAR>conn</VAR>, <VAR>address</VAR>)</CODE>
- where <VAR>conn</VAR> is a <I>new</I> socket object usable to send and
- receive data on the connection, and <VAR>address</VAR> is the address bound
- to the socket on the other end of the connection.
- </DL>
- <DL><DT><B>bind</B> (<VAR>address</VAR>) -- Method on socket<DD>
- Bind the socket to <VAR>address</VAR>. The socket must not already be bound.
- (The format of <VAR>address</VAR> depends on the address family --- see above.)
- </DL>
- <DL><DT><B>close</B> () -- Method on socket<DD>
- Close the socket. All future operations on the socket object will fail.
- The remote end will receive no more data (after queued data is flushed).
- Sockets are automatically closed when they are garbage-collected.
- </DL>
- <DL><DT><B>connect</B> (<VAR>address</VAR>) -- Method on socket<DD>
- Connect to a remote socket at <VAR>address</VAR>.
- (The format of <VAR>address</VAR> depends on the address family --- see above.)
- </DL>
- <DL><DT><B>fileno</B> () -- Method on socket<DD>
- Return the socket's file descriptor (a small integer). This is useful
- with <CODE>select</CODE>.
- </DL>
- <DL><DT><B>getpeername</B> () -- Method on socket<DD>
- Return the remote address to which the socket is connected. This is
- useful to find out the port number of a remote IP socket, for instance.
- (The format of the address returned depends on the address family ---
- see above.) On some systems this function is not supported.
- </DL>
- <DL><DT><B>getsockname</B> () -- Method on socket<DD>
- Return the socket's own address. This is useful to find out the port
- number of an IP socket, for instance.
- (The format of the address returned depends on the address family ---
- see above.)
- </DL>
- <DL><DT><B>getsockopt</B> (<VAR>level</VAR>, <VAR>optname</VAR>[, <VAR>buflen</VAR>]) -- Method on socket<DD>
- Return the value of the given socket option (see the UNIX man page
- <I>getsockopt</I>(2)). The needed symbolic constants (<CODE>SO_*</CODE> etc.)
- are defined in this module. If <VAR>buflen</VAR>
- is absent, an integer option is assumed and its integer value
- is returned by the function. If <VAR>buflen</VAR> is present, it specifies
- the maximum length of the buffer used to receive the option in, and
- this buffer is returned as a string. It is up to the caller to decode
- the contents of the buffer (see the optional built-in module
- <CODE>struct</CODE> for a way to decode C structures encoded as strings).
- </DL>
- <DL><DT><B>listen</B> (<VAR>backlog</VAR>) -- Method on socket<DD>
- Listen for connections made to the socket. The <VAR>backlog</VAR> argument
- specifies the maximum number of queued connections and should be at
- least 1; the maximum value is system-dependent (usually 5).
- </DL>
- <DL><DT><B>makefile</B> ([<VAR>mode</VAR>[, <VAR>bufsize</VAR>]]) -- Method on socket<DD>
- Return a <DFN>file object</DFN> associated with the socket. (File objects
- were described earlier under Built-in Types.) The file object
- references a <CODE>dup()</CODE>ped version of the socket file descriptor, so
- the file object and socket object may be closed or garbage-collected
- independently. The optional <VAR>mode</VAR> and <VAR>bufsize</VAR> arguments
- are interpreted the same way as by the built-in
- <CODE>open()</CODE> function.
- </DL>
- <DL><DT><B>recv</B> (<VAR>bufsize</VAR>[, <VAR>flags</VAR>]) -- Method on socket<DD>
- Receive data from the socket. The return value is a string representing
- the data received. The maximum amount of data to be received
- at once is specified by <VAR>bufsize</VAR>. See the UNIX manual page
- for the meaning of the optional argument <VAR>flags</VAR>; it defaults to
- zero.
- </DL>
- <DL><DT><B>recvfrom</B> (<VAR>bufsize</VAR>[, <VAR>flags</VAR>]) -- Method on socket<DD>
- Receive data from the socket. The return value is a pair
- <CODE>(<VAR>string</VAR>, <VAR>address</VAR>)</CODE> where <VAR>string</VAR> is a string
- representing the data received and <VAR>address</VAR> is the address of the
- socket sending the data. The optional <VAR>flags</VAR> argument has the
- same meaning as for <CODE>recv()</CODE> above.
- (The format of <VAR>address</VAR> depends on the address family --- see above.)
- </DL>
- <DL><DT><B>send</B> (<VAR>string</VAR>[, <VAR>flags</VAR>]) -- Method on socket<DD>
- Send data to the socket. The socket must be connected to a remote
- socket. The optional <VAR>flags</VAR> argument has the same meaning as for
- <CODE>recv()</CODE> above. Return the number of bytes sent.
- </DL>
- <DL><DT><B>sendto</B> (<VAR>string</VAR>[, <VAR>flags</VAR>], <VAR>address</VAR>) -- Method on socket<DD>
- Send data to the socket. The socket should not be connected to a
- remote socket, since the destination socket is specified by
- <CODE>address</CODE>. The optional <VAR>flags</VAR> argument has the same
- meaning as for <CODE>recv()</CODE> above. Return the number of bytes sent.
- (The format of <VAR>address</VAR> depends on the address family --- see above.)
- </DL>
- <DL><DT><B>setblocking</B> (<VAR>flag</VAR>) -- Method on socket<DD>
- Set blocking or non-blocking mode of the socket: if <VAR>flag</VAR> is 0,
- the socket is set to non-blocking, else to blocking mode. Initially
- all sockets are in blocking mode. In non-blocking mode, if a
- <CODE>recv</CODE> call doesn't find any data, or if a <CODE>send</CODE> call can't
- immediately dispose of the data, a <CODE>socket.error</CODE> exception is
- raised; in blocking mode, the calls block until they can proceed.
- </DL>
- <DL><DT><B>setsockopt</B> (<VAR>level</VAR>, <VAR>optname</VAR>, <VAR>value</VAR>) -- Method on socket<DD>
- Set the value of the given socket option (see the UNIX man page
- <I>setsockopt</I>(2)). The needed symbolic constants are defined in
- the <CODE>socket</CODE> module (<CODE>SO_*</CODE> etc.). The value can be an
- integer or a string representing a buffer. In the latter case it is
- up to the caller to ensure that the string contains the proper bits
- (see the optional built-in module
- <CODE>struct</CODE> for a way to encode C structures as strings).
- </DL>
- <DL><DT><B>shutdown</B> (<VAR>how</VAR>) -- Method on socket<DD>
- Shut down one or both halves of the connection. If <VAR>how</VAR> is <CODE>0</CODE>,
- further receives are disallowed. If <VAR>how</VAR> is <CODE>1</CODE>, further sends are
- disallowed. If <VAR>how</VAR> is <CODE>2</CODE>, further sends and receives are
- disallowed.
- </DL>
- Note that there are no methods <CODE>read()</CODE> or <CODE>write()</CODE>; use
- <CODE>recv()</CODE> and <CODE>send()</CODE> without <VAR>flags</VAR> argument instead.
- <P>
-