Wednesday 26 February 2014

How To Do Socket Programming In Python


Python’s socket  module is just a thin wrapper (interface layer) over the underlying C library’s socket calls. Like Python files, it’s also object-based—methods of a socket object implemented by this module call out to the corresponding C library’s operations after data conversions. For instance, the C library’s send  and recv  function calls become methods of socket objects in Python.
Beyond basic data communication tasks, the socket  module also includes a variety of
more advanced tools. For instance, it has calls for the following and more:

• Converting bytes to a standard network ordering (ntohl , htonl )
• Querying machine name and address (gethostname , gethostbyname )
• Wrapping socket objects in a file object interface (sockobj.makefile )
• Making socket calls nonblocking (sockobj.setblocking )
• Setting socket timeouts (sockobj.settimeout )


Python programs import the socket  module, create a socket object, and call the object’s methods to establish connections and send and receive data. Sockets are inherently bidirectional in nature, and socket object methods map directly to socket calls in the C library. 
Following example  implements a program(echo-server.py) that simply listens for a connection on a socket and echoes back over a socket whatever it receives through that socket, adding Echo=>  string prefixes.
"""
Server side: open a TCP/IP socket on a port, listen for a message from a client, andsend an echo reply; this is a simple one-shot listen/reply conversation per client, but it goes into an infinite loop to listen for more clients as long as this server script runs; the client may run on a remote machine, or on same computer if it uses 'localhost' for server
""" from socket import * # get socket constructor and constants myHost = '' # '' = all available interfaces on host myPort = 50007 # listen on a non-reserved port number sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP socket object sockobj.bind((myHost, myPort)) # bind it to server port number sockobj.listen(5) # listen, allow 5 pending connects while True: # listen until process killed connection, address = sockobj.accept() # wait for next client connect print('Server connected by', address) # connection is a new socket while True: data = connection.recv(1024) # read next line on client socket if not data: break # send a reply line to the client connection.send(b'Echo=>' + data) # until eof when socket closed connection.close()
"""
Client side: use sockets to send data to the server, and print server's reply to each message line; 'localhost' means that the server is running on the same machine as the client, which lets us test client and server on one machine; to test over the Internet, run a server on a remote machine, and set serverHost or argv[1] to machine'sdomain name or IP addr; Python sockets are a portable BSD socket interface, with object methods for the standard socket calls available in the system's C library;
""" import sys from socket import * # portable socket interface plus constants serverHost = 'localhost' # server name, or: 'starship.python.net' serverPort = 50007 # non-reserved port used by the server message = [b'Hello network world'] # default text to send to server # requires bytes: b'' or str,encode() if len(sys.argv) > 1: serverHost = sys.argv[1] # server from cmd line arg 1 if len(sys.argv) > 2: # text from cmd line args 2..n message = (x.encode() for x in sys.argv[2:]) sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP/IP socket object sockobj.connect((serverHost, serverPort)) # connect to server machine + port for line in message: sockobj.send(line) # send line to server over socket data = sockobj.recv(1024) # receive line from server: up to 1k print('Client received:', data) # bytes are quoted, was `x`, repr(x) sockobj.close() # close socket to send eof to server