| < Day Day Up > |
|
Like many of the other object-oriented scripting languages, Python includes a standard Sockets layer (built into classes) and high-level APIs, providing methods to perform the standard Application layer protocols (HTTP, FTP, SMTP, and so on).
We’ll divide Python’s socket library into two distinct parts. The first are the class methods that require no instance of a socket class. For example, when creating a new socket, we’ll have no instance to go with. Therefore, we’ll use the socket class directly, specifically the socket method. The second are instance methods that require an instance of a socket. For example, to close a socket, we must have first created one. Therefore, the close method operates on an existing instance of the socket class. The class methods are shown in Figure 11.1 and the instance methods are listed in Figure 11.2.
Method | Class | Description |
---|---|---|
socket( [family[, type[, proto]]] ) | socket | Create a new socket of the specified type. |
gethostbyaddr( host ) | socket | Return the hostname for a given IP address. |
gethostbyname( host ) | socket | Return the string IP address for a hostname. |
gethostname( ) | socket | Return the string hostname for the current host. |
getservbyname( servicename, protocolname ) | socket | Return the port number for a given string service. |
htonl( integer ) | socket | Convert from host to network 32-bit. |
ntohl( integer ) | socket | Convert from network to host 32-bit. |
hton( short ) | socket | Convert from host to network 16-bit. |
ntohs( short ) | socket | Convert from network to host 16-bit. |
inet_aton( string ) | socket | Convert a string IP address to binary format. |
inet_ntoa( string ) | socket | Convert a binary format string address to a string. |
Method | Type | Description |
---|---|---|
accept( ) | Instance | Wait for incoming connections on a server socket instance. |
bind( address ) | Instance | Bind the socket instance to a local address. |
close( ) | Instance | Close the socket instance. |
connect( address ) | Instance | Connect the socket instance to the remove address. |
fileno( ) | Instance | Return the integer file descriptor for a socket instance. |
getpeername( ) format. | Instance | Return the address of the remove endpoint in (host, port). |
getsockname( ) | Instance | Return the address of the local socket in (host, port) format. |
getsockopt( level, option[, size] )) | Instance | Return the value of the socket option for the socket instance. |
setsockopt( level, option, value ) | Instance | Set the value of a socket option for a socket instance. |
listen( backlog ) | Instance | Enable the server socket to listen for incoming connections. |
recv( size[, flags] ) | Instance | Receive up to size bytes from the peer socket. |
recvfrom( size[, flags] ) | Instance | Receive up to size bytes. Response is (data, address). |
send( data[, flags] ) | Instance | Send the data bytes to the connected peer. |
sendto( data[, flags], address ) | Instance | Send the data bytes in datagram format to the peer defined by address. |
setblocking( flag ) | Instance | Set the socket to blocking or non-blocking (true/false). |
shutdown( flag ) | Instance | Shutdown the socket (0=read, 1=write, 2=both). |
| < Day Day Up > |
|