Module _socket :: Class socket
[hide private]
[frames] | no frames]

type socket

object --+
         |
        socket

socket([family[, type[, proto]]]) -> socket object

Open a socket of the given type.  The family argument specifies the
address family; it defaults to AF_INET.  The type argument specifies
whether this is a stream (SOCK_STREAM, this is the default)
or datagram (SOCK_DGRAM) socket.  The protocol argument defaults to 0,
specifying the default protocol.  Keyword arguments are accepted.

A socket object represents one endpoint of a network connection.

Methods of socket objects (keyword arguments not allowed):

accept() -- accept a connection, returning new socket and client address
bind(addr) -- bind the socket to a local address
close() -- close the socket
connect(addr) -- connect the socket to a remote address
connect_ex(addr) -- connect, return an error code instead of an exception
dup() -- return a new socket object identical to the current one [*]
fileno() -- return underlying file descriptor
getpeername() -- return remote address [*]
getsockname() -- return local address
getsockopt(level, optname[, buflen]) -- get socket options
gettimeout() -- return timeout or None
listen(n) -- start listening for incoming connections
makefile([mode, [bufsize]]) -- return a file object for the socket [*]
recv(buflen[, flags]) -- receive data
recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)
recvfrom(buflen[, flags]) -- receive data and sender's address
recvfrom_into(buffer[, nbytes, [, flags])
  -- receive data and sender's address (into a buffer)
sendall(data[, flags]) -- send all data
send(data[, flags]) -- send data, may not send all of it
sendto(data[, flags], addr) -- send data to a given address
setblocking(0 | 1) -- set or clear the blocking I/O flag
setsockopt(level, optname, value) -- set socket options
settimeout(None | float) -- set or clear the timeout
shutdown(how) -- shut down traffic in one or both directions

 [*] not available on all platforms!

Instance Methods [hide private]
 
__getattribute__(...)
x.__getattribute__('name') <==> x.name
socket object

__init__(family=..., type=..., proto=...=...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
a new object with type S, a subtype of T
__new__(T, S, ...)
 
__repr__(x)
repr(x)
(socket object, address info)
accept()
Wait for an incoming connection.
 
bind(address)
Bind the socket to a local address.
 
close()
Close the socket.
 
connect(address)
Connect the socket to a remote address.
errno
connect_ex(address)
This is like connect(address), but returns an error code (the errno value) instead of raising an exception when an error occurs.
socket object
dup()
Return a new socket object connected to the same system resource.
integer
fileno()
Return the integer file descriptor of the socket.
address info
getpeername()
Return the address of the remote endpoint.
address info
getsockname()
Return the address of the local endpoint.
value
getsockopt(level, option, buffersize=...)
Get a socket option.
timeout
gettimeout()
Returns the timeout in floating seconds associated with socket operations.
 
listen(backlog)
Enable a server to accept connections.
file object
makefile(mode=..., buffersize=...)
Return a regular file object corresponding to the socket.
data
recv(buffersize, flags=...)
Receive up to buffersize bytes from the socket.
 
recv_into(...)
recv_into(buffer, [nbytes[, flags]]) -> nbytes_read
(data, address info)
recvfrom(buffersize, flags=...)
Like recv(buffersize, flags) but also return the sender's address info.
(nbytes, address info)
recvfrom_into(buffer, nbytes=..., flags=...)
Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.
count
send(data, flags=...)
Send a data string to the socket.
 
sendall(data, flags=...)
Send a data string to the socket.
 
sendto(...)
sendto(data[, flags], address) -> count
 
setblocking(flag)
Set the socket to blocking (flag is true) or non-blocking (false).
 
setsockopt(level, option, value)
Set a socket option.
 
settimeout(timeout)
Set a timeout on socket operations.
 
shutdown(flag)
Shut down the reading side of the socket (flag == SHUT_RD), the writing side of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).
Properties [hide private]
  family
the socket family
  proto
the socket protocol
  timeout
the socket timeout
  type
the socket type
Method Details [hide private]

__getattribute__(...)

 

x.__getattribute__('name') <==> x.name

Overrides: object.__getattribute__

__init__(family=..., type=..., proto=...=...)
(Constructor)

 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Returns:
socket object

Overrides: object.__init__

__new__(T, S, ...)

 
Returns: a new object with type S, a subtype of T
Overrides: object.__new__

__repr__(x)
(Representation operator)

 

repr(x)

Overrides: object.__repr__

accept()

 

Wait for an incoming connection. Return a new socket representing the connection, and the address of the client. For IP sockets, the address info is a pair (hostaddr, port).

Returns: (socket object, address info)

bind(address)

 

Bind the socket to a local address. For IP sockets, the address is a pair (host, port); the host must refer to the local host. For raw packet sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])

close()

 

Close the socket. It cannot be used after this call.

connect(address)

 

Connect the socket to a remote address. For IP sockets, the address is a pair (host, port).

getpeername()

 

Return the address of the remote endpoint. For IP sockets, the address info is a pair (hostaddr, port).

Returns: address info

getsockname()

 

Return the address of the local endpoint. For IP sockets, the address info is a pair (hostaddr, port).

Returns: address info

getsockopt(level, option, buffersize=...)

 

Get a socket option. See the Unix manual for level and option. If a nonzero buffersize argument is given, the return value is a string of that length; otherwise it is an integer.

Returns: value

gettimeout()

 

Returns the timeout in floating seconds associated with socket operations. A timeout of None indicates that timeouts on socket operations are disabled.

Returns: timeout

listen(backlog)

 

Enable a server to accept connections. The backlog argument must be at least 1; it specifies the number of unaccepted connection that the system will allow before refusing new connections.

makefile(mode=..., buffersize=...)

 

Return a regular file object corresponding to the socket. The mode and buffersize arguments are as for the built-in open() function.

Returns: file object

recv(buffersize, flags=...)

 

Receive up to buffersize bytes from the socket. For the optional flags argument, see the Unix manual. When no data is available, block until at least one byte is available or until the remote end is closed. When the remote end is closed and all data is read, return the empty string.

Returns: data

recv_into(...)

 

recv_into(buffer, [nbytes[, flags]]) -> nbytes_read

A version of recv() that stores its data into a buffer rather than creating a new string. Receive up to buffersize bytes from the socket. If buffersize is not specified (or 0), receive up to the size available in the given buffer.

See recv() for documentation about the flags.

send(data, flags=...)

 

Send a data string to the socket. For the optional flags argument, see the Unix manual. Return the number of bytes sent; this may be less than len(data) if the network is busy.

Returns: count

sendall(data, flags=...)

 

Send a data string to the socket. For the optional flags argument, see the Unix manual. This calls send() repeatedly until all data is sent. If an error occurs, it's impossible to tell how much data has been sent.

sendto(...)

 

sendto(data[, flags], address) -> count

Like send(data, flags) but allows specifying the destination address. For IP sockets, the address is a pair (hostaddr, port).

setblocking(flag)

 

Set the socket to blocking (flag is true) or non-blocking (false). setblocking(True) is equivalent to settimeout(None); setblocking(False) is equivalent to settimeout(0.0).

setsockopt(level, option, value)

 

Set a socket option. See the Unix manual for level and option. The value argument can either be an integer or a string.

settimeout(timeout)

 

Set a timeout on socket operations. 'timeout' can be a float, giving in seconds, or None. Setting a timeout of None disables the timeout feature and is equivalent to setblocking(1). Setting a timeout of zero is the same as setblocking(0).