How to Set up a Socket Connection in Python 


               


How to Set up a Socket Connection in Python



Python


In this article, we show how to set up a socket connection in Python using the socket module.

So the socket module in Python can be thought of as a module that lets different networks talk to one another.

For example, when you go to a web browser and type in a website, that browser opens up a socket connection with the server storing that website in order to retrieve the page. So the browser is the client and the web hosting company storing that website is the server. A socket is opened on both sides, so that both sides can communicate with one another.

This is a typical reason for a socket connection.

The default port at which sockets communicate via the world wide web is port 80.

There are other various ports for communication.

Another common port used in network communication is port 587 for email service providers using the SMTP (simple mail transfer protocol).

So sockets allow networks to communicate. The port that is used depends on the type of application that is being used. HTTP generally uses port 80 for communication. SMTP uses ports such as port 587. And so on and so forth.

So now you know that sockets allow networks to communicate.

The port that you use depends on the type of softwaqre application you are using.

If you simply want to request a page from a web server, then you want to use the default port of port 80.

This is what we'll do in the following code below.

We will mimic a browser and communicate with a web server. We will request the home page for this website, learningaboutelectronics.com from the server.

We do this using sockets.

Sockets is just 2 computers (or 2 networks) communicating together. In order for sockets to communicate, they need to use a common port.



So the first thing we have to do is import the socket module, which we do with the line, import socket

We then create a socket object with the line, s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)

We then create a variable, server, and assign to the value, 'learningaboutelectronics.com'

This is because we want to request a page from the website, learningaboutelectronics.com

We then set the port from which the socket will communicate. Again, the default port of HTTP, the application language in which browsers and servers communicate, is port 80. Therefore, we set our port to port 80.

We then create a variable, request, and assign it to, "GET / HTTP/1.1\nHost: " + server + "\n\n"

What this line does is it creates a HTTP GET request, in which we request the home page of the website, learningaboutelectronics.com

We use HTTP version 1.1

We then connect to the server through port 80 with the s.connect() function.

We have to encode the request because in Python 3, requests must be sent through byte-strings, not simply bits. So we have to use the encode() function. In Python 2, this wouldn't be needed, but Python 3 is different.

We then have to specify how many bytes we want to receive. We specify 7000.

The output will be a bit garbled and hard to understand, but it is the home page of learningaboutelectronics.com

We get an HTTP response of 200, which means the page request is successful.

We get the date of the request.

And we get that the server is Apache.

And we get that the page we requested is an HTML document.

So we've replicated a browser program in which we have requested a page from a web server and returned the page, all while using sockets in Python with the socket module and communicating with a standard port.

And this is how to set up a socket connection in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...