Networking Programming In Python

In this chapter, you will learn the concept of networking programming like sockets and ports etc.

In Python programming language, there is a two level access to network Services at a low level. We can access the basic socket support in an operating system, which supports to implement the Server oriented and client oriented connection protocol.

Python also has a library to provide a high level access such as FTP and HTTP etc. network protocol.

  • Sockets are by direction endpoint communication channel.
  • Sockets can implement a different channel like-
    • Unix domain sockets
    • TCP
    • UDP
Some terms, which can be used in networking programming are-

Domain- Set of protocol, which can be used as the transport mechanism

Type- Type of communication between the two end points.

Protocol- Used to identify the protocol of domain and type.

Hostname- Identifier of a network interface.

Port -Each Server listener for clients calls on one or more port.

Socket Module-

In Python, programming language can create the syntax, given below-


s = socket.socket (socket_family, socket_type, protocol=0)

Server Socket Methods-

Method Description
s.bind()This method is used to bind the address (hostname, port number pair) to socket. 
s.listen() This method used to set up and start TCP listener.
s.accept()  This method is used to accept TCP client connection, waiting until connection blocking.

Client Socket Methods-

 Method Description
 s.connect() This method used to establish TCP Server connection.

General Socket Methods-

s.recv()This method is used to receive TCP message. 
This method is used to receive transmits TCP message. 
s.recvfrom() This method is used to receive UDP message.
s.sendto()This method is used to receive transmitted UDP message.
s.close() This method is used to receive closes socket.
socket.gethostname()This method is used to returns the hostname.

Example of Simple Server-

  1. import socket  
  2. serversocket = socket.socket(  
  3.            socket.AF_INET, socket.SOCK_STREAM)  
  4. host = socket.gethostname()  
  5. port = 9999  
  6. serversocket.bind((host, port))  
  7. serversocket.listen(5)  
  8. while True:  
  9.           clientsocket,addr = serversocket.accept()  
  10.           print("connected %s" % str(addr))  
  11.            msg='thank you'"\r\n"  
  12.            clientsocket.send(msg.encode('ascii'))  
  13.            clientsocket.close()  
Example of Simple client
  1. import socket  
  2. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
  3. host = socket.gethostname()  
  4. port = 9999  
  5. s.connect((host, port))  
  6. msg = s.recv(1024)  
  7. s.close()  
  8. print (msg.decode('ascii'))  
Python Internet modules
 Protocol Common functionPort No Python module 
HTTP Web pages 80 httplib, urllib, xmlrpclib
NNTPUsenet news119nntplib
FTP File transfers 20ftplib, urllib
SMTPSending email 25smtplib 
POP3 Fetching email 110poplib
IMAP4Fetching email 143 imaplib
TelnetCommand lines23 telnetlib
Gopher Document transfers 70 gopherlib, urllib 


In this chapter you learnt the concept of networking programming like sockets and ports etc.
Next » GUI Programming In Python