Python Quick Notes :: Part - 5
Bhaskar S | 03/16/2013 |
Hands-on With Python - V
Python provides excellent support for network programming using the socket class from the Python module socket. The following are some of the frequently used functions from socket class:
socket.gethostname() - returns the host name of the machine on which this method is executed
socket.gethostbyname(host) - returns the IP address corresponding to the specified host as a string in the form A.B.C.D
socket.gethostbyaddr(ip) - returns a tuple of 3 items (host, aliases, ipaddrs) where host is the primary host name, aliases is a list of other names by which the host is known, and ipaddrs is the list of other ip addresses for the host on the same interface
socket.socket(socket.AF_INET, type) - create a new socket object for the specified type which can be socket.SOCK_STREAM for TCP or socket.SOCK_DGRAM for UDP
socket.bind(address) - bind the socket to the address which is a tuple of 2 items - the host and the port. This method is called in the server code
socket.listen(backlog) - listen for incoming connections on the socket where backlog is the maximum number of queued connections. This method is called in the server code
(clientsocket, address) = socket.accept() - accept the incoming connection on the socket and return a tuple of 2 items - clientsocket is the socket corresponding to the client that made the connection and address is the address (a tuple of 2 items - the host and the port) corresponding to the client. This method is called in the server code
socket.connect(address) - connect to the server with address which is a tuple of 2 items - the host and the port. This method is called in the client code
socket.recv(size) - Receive data from a TCP socket. The maximum amount of data to be read is specified by size
bytes = socket.send(data) - write the specified data to the socket. Returns the number of actual bytes sent
(bytes, address) = socket.recvfrom(size) - Receive data from an UDP socket. The maximum amount of data to be read is specified by size. Returns a tuple of 2 items - the number of actual bytes read and the address (a tuple of 2 items - the host and the port) from which the data was read
bytes = socket.sendto(data, address) - write the specified data to the specified address (a tuple of 2 items - the host and the port). Returns the number of actual bytes sent
socket.close() - close the socket
The following is the python program named sample-23.py:
Execute the following command:
python sample-23.py
The following is the output:
Machine host name: nutcracker Machine IP address: 127.0.1.1 www.google.com's IP address: 173.194.75.103 Aliases: [] IP address(es): ['127.0.1.1'] Aliases: [] IP address(es): ['173.194.75.103']
We will now test a simple TCP server and client. First start the server and then the client.
The following is the simple python TCP server program named sample-24.py:
Execute the following command:
python sample-24.py
The following is the output:
---> Created TCP server socket ..... ---> Bound to address ('localhost', 9090) ---> TCP Server socket ready and listening .....
The following is the simple python TCP client program named sample-25.py:
Execute the following command:
python sample-25.py
The following is the output:
---> Created TCP client socket ..... ---> Connected to TCP server at address ('localhost', 9090) ---> Sent 12 bytes of data to the server ('localhost', 9090) ---> Received data from server ('localhost', 9090): TCP Server Replay: Hello Python
Next, we will now test a simple UDP server and client. First start the server and then the client.
The following is the simple python UDP server program named sample-26.py:
Execute the following command:
python sample-26.py
The following is the output:
---> Created UDP server socket ..... ---> Bound to address ('localhost', 9091)
The following is the simple python UDP client program named sample-27.py:
Execute the following command:
python sample-27.py
The following is the output:
---> Created UDP client socket ..... ---> Sent 12 bytes of data to the server ('localhost', 9091) ---> Received data from server ('127.0.0.1', 9091): UDP Server Replay: Hello Python
Python provides support for accessing web resources using the Python module urllib.
The following is the python program named sample-28.py:
Execute the following command:
python sample-28.py
The following is the output:
URL: http://www.google.com/ Response data length: 10846 HTTP Response Headers: ---------------------- Date: Sun, 17 Mar 2013 18:44:40 GMT Expires: -1 Cache-Control: private, max-age=0 Content-Type: text/html; charset=ISO-8859-1 Set-Cookie: PREF=ID=e675c66ab3053b32:FF=0:TM=1363545880:LM=1363545880:S=jcSLoyehfQDvCTRM; expires=Tue, 17-Mar-2015 18:44:40 GMT; path=/; domain=.google.com Set-Cookie: NID=67=o41thIplYdRdA-K4zczsDt3P4PzvTZkFrOW94MtKbVHWShlfwWmsjfNigJ7YnleQpFuTDtgx3s_FHCzzI4_opSy_1yGcm8eyQpsf7Mg_uVsXamS-XKUZhc7NuFnb5AYk; expires=Mon, 16-Sep-2013 18:44:40 GMT; path=/; domain=.google.com; HttpOnly P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." Server: gws X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN
References