Python, in default offers low-level access to network services as many other languages do. This is often required when developing client-server applications or an application which needs to communicate with another service. Therefore in this article, it is discussed, how socket programming can be achieved in Python.
In this approach, python library socket is used. It is a part of the standard Python library, hence no manual installation is required.
This is a simple server which keeps listening to an incoming traffic from a client and then respond.
#!/usr/bin/python import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) HOST = '127.0.0.1' PORT = 30303 BUFFER_SIZE = 2014 s.bind((HOST, PORT)) s.listen(5) while True: print ('server running --') conn, addr = s.accept() data = conn.recv(BUFFER_SIZE) print ('Got connection from ', data, '-', addr) conn.send(b'Thank you for connecting') conn.close()
Note how the socket object has been initialized and then HOST address and the PORT have been defined. bind( ) method reserves the port for the host. accept( ) method accepts the incoming connection and then output another instance of the socket object as conn and the addr holds the address (and the port) of the incoming request.
When sending data using send( ) method, the content should be a byte-like object. No string objects can be used. That is why b literal is used. Or string.encode( ) can also be used here. The rest is pretty straight forward.
Following is the code snippet for the client.
Code is almost as same as the server code other than not having a loop to listen to the incoming traffic. It simply send some data to the server and then receives the output from the server.
#!/usr/bin/python import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) HOST = '127.0.0.1' PORT = 30303 s.connect((HOST, PORT)) s.send(b'username:user') data = s.recv(1024) print (data) s.close ()
The server code needs to be executed first and then the client code.
However, since there is no threads in the server code, only 1 connection can be established with the server at a time.
Requesting a web page
It is possible to use the client code to contact a web server (or any other service) and request a web page. Or even contact a FTP service. The send request needs to be modified according to the protocol.
Following is a simple example on getting a web page using the client code. Localhost is used in the code however the HOST can be replaced with any URL which supports HTTP.
#!/usr/bin/python import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) HOST = '127.0.0.1' PORT = 80 s.connect((HOST, PORT)) s.send(b'GET /test/index.php HTTP/1.1\n' b'Host: 127.0.0.1\n' b'Connection: keep-alive\n' b'Upgrade-Insecure-Requests: 1\n' b'\r\n') data = s.recv(1024) print (data) s.close ()
Notice that only the send method has been changed to facilitate HTTP protocol. The output is a plain-text including header and body content. Further processing is required.
For HTTP protocol, httplib module can be used too. However it is not a part of the standard Python library and it is required to get it installed manually. In the same manner, few non-standard libraries are available for FTP (ftplib), SMTP (smtplib), POP3 (poplib) protocols.
This is a simple introduction on socket programming in Python. According to your requirement the code needs to be modified. The intention was to demonstrate how Python can be used for the above mentioned purpose.