Python delivers two levels of right-to-network services. At a low level, you can access the elementary socket reinforcing in the underlying operating system, which authorizes you to utilize clients and servers for both connection-oriented and connection-less protocols. Python is loaded with wide libraries supplying exact protocols like FTP, HTTP, etc high-level application network programming. This chapter delivers you an understanding of the most illustrious concept in Networking - Socket Programming.
Sockets are considered as the last points of a bi-directional statements channel. Sockets might converse within a procedure, between courses on the aforementioned machine, or amid processes on dissimilar continents.
Sockets might be put into practice over several assorted channel kinds: Unix domain sockets, TCP, UDP, etc. The socket library offers exact classes for running the ordinary transports and a general interface for managing the rest.
Do sockets have their own vocabulary?
Term & Description
The family of protocols that is used as the transport mechanism. These values are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.
The type of communications between the two endpoints, typically SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connection-less protocols.
Typically zero, this may be utilized to identify a variable of a protocol within a domain and type.
The identifier of a network interface?
A string, which can be a host name, a dotted-quad address, or an IPV6 address in colon (and possibly dot) notation
A string "", which specifies an INADDR_BROADCAST address.
A zero-length string, which specifies INADDR_ANY, or
An Integer, interpreted as a binary address in host byte order.
Each server listens for clients calling on one or more ports. A port may be a Fixnum port number, a string comprising a port number, or the name of a service.
To make a socket, you must utilize the socket.socket() function easy to get to in socket module, which has the universal syntax:
s = socket.socket (socket_family, socket_type, protocol=0)
Here is the description of the parameters-
Once users possess socket objects, in such scenarios, users could use the needed functions to create a client or server program.
Following is the list of functions required-
Method & Description
This method binds the address (hostname, port number pair) to the socket.
This method sets up and starts a TCP listener.
This passively accepts TCP client connection, waiting until a connection arrives (blocking).
Method & Description
This method actively initiates TCP server connection.
Method & Description
This method receives TCP message
This method transmits TCP message
This method receives UDP message
This method transmits UDP message
This method closes socket
Returns the hostname.
To write Internet servers, programmers need to utilize the socket function obtainable in the socket module to develop a socket object. A socket object is then utilized to call additional functions to system a socket server.
Now call bind(hostname, port) function to state a port for service on the provided host.
Next, call the accept method of the returned object. This method stays unless a client attaches to the port that users specified, and then returns a connection object that depicts the link to that client.
#!/usr/bin/python # This is server.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print 'Got connection from', addr c.send('Thank you for connecting') c.close() # Close the connection
Let us write a simple client program that unlocks a connection supposing to a provided port 54321 and host. This is simple to expand a socket user using Python's socket module function.
The socket.connect(hosname, port ) unbolts a TCP connection to hostname on the port. Once users possess a socket open, then they could interpret from accepting as IO object. When completed, keep in mind to close it, as users need to close a file.
The following code is a simple client that connects to a provided host and port, reads any available data from the socket, and then exits-
#!/usr/bin/python # This is client.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.connect((host, port)) print s.recv(1024) s.close() # Close the socket when done
Now run this server.py in the background and then run above client.py to see the result.
# Following would start a server in background. $ python server.py & # Once server is started run client as follows: $ python client.py
This would supply the following results:
Got connection from ('127.0.0.1', 48437) Thank you for connecting
A list of some important modules in Python Network/Internet programming.
httplib, urllib, xmlrpclib
Please check all the libraries mentioned above to work with FTP, SMTP, POP, and IMAP protocols.
Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.