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
- Sockets are by direction endpoint communication channel.
- Sockets can implement a different channel like-
- Unix domain sockets
- TCP
- UDP
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-
Syntax
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-
Method | Description |
s.recv() | This method is used to receive TCP message. |
s.send() | 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-
- import socket
- serversocket = socket.socket(
- socket.AF_INET, socket.SOCK_STREAM)
- host = socket.gethostname()
- port = 9999
- serversocket.bind((host, port))
- serversocket.listen(5)
- while True:
- clientsocket,addr = serversocket.accept()
- print("connected %s" % str(addr))
- msg='thank you'+ "\r\n"
- clientsocket.send(msg.encode('ascii'))
- clientsocket.close()
Example of Simple client
- import socket
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- host = socket.gethostname()
- port = 9999
- s.connect((host, port))
- msg = s.recv(1024)
- s.close()
- print (msg.decode('ascii'))
Python Internet modules
Protocol | Common function | Port No | Python module |
HTTP | Web pages | 80 | httplib, urllib, xmlrpclib |
NNTP | Usenet news | 119 | nntplib |
FTP | File transfers | 20 | ftplib, urllib |
SMTP | Sending email | 25 | smtplib |
POP3 | Fetching email | 110 | poplib |
IMAP4 | Fetching email | 143 | imaplib |
Telnet | Command lines | 23 | telnetlib |
Gopher | Document transfers | 70 | gopherlib, urllib |
Summary
In this chapter you learnt the concept of networking programming like sockets and ports etc.