Write a TCP Server in Python – Part 2
This article is for people that have good knowledge in Python and want to learn and practise new skills and do some network basics. Information about sockets is very important when using Python for network scripting. Before we can create and use sockets we have to import the socket library as we explained and showed in Part 1 of this series.
If you have been introduced to my articles about basics in Python you know that we have worked in interactive mode, but now is time to learn and create scripts. To create scripts, we need a text editor and after we finish writing the code we save the file with the extension .py which stands for Python. The text editor I will use is called vim, which is almost compatible version of the Unix editor vi and I think that it’s a well known text editor to Linux geeks. I must say I love this text editor because it offers a beautiful syntax highlighting that makes my code easy to read and friendly to other people.
The first line we will write is called the shebang line which indicates what interpreter to use. This line is necessary only in Unix base platforms such as Linux, Mac OS and does not apply to other platforms. We will use Python interpreter to interpret our Python scripts so the shebang line will be #!/usr/bin/Python like shown in Figure1. If you have problems with vim and never used it before feel free to post you question in the comment section or you can use a text editor of your choice.
After we finish creating the shebang line we import the socket library like shown in Figure 2.
Now we can create a socket object. The variable I will use is called my_socket. How does the client identify the server and connects to it? The server has an IP address like houses have addresses. Since we will run our script in our local machine , the IP address will be 127.0.0.1 aka localhost. Our server should listen on some port, so we need a port number too.
Figure 3 shows how to create the socket object which we assign to my_socket variable and two other variables, one for the hostname of the server, one for the port in which the server will listen for connections.
Time to bind and listen, to do that we use the bind method and listen method like shown in Figure 4. The bind() method accepts a tuple with two elements, the server name and the port on which the server will listen for incoming connections. We know that when used inside expressions variables return their actual values so we use use our variables, hostname and port_number as elements for the tuple in bind() method. The listen() method sets up and start TCP listener.
We want our server to listen all the time, day and night. How do we do that in Python? To do that we need a condition that returns always true bolean value when tested. Open a new terminal in your Linux machine, type Python (interactive mode) and write the code exactly as shown in Figure 5.
Now press enter and see what happens (Figure 6). This simple program prints ‘unixmen’ string infinitly. This is what we need for the server. We will use the while conditional statement because it returns the ‘true’ bolean value all the time. Our server will accept client requests all the time so we need a while statement that is always true. Figure 7 shows the finished server and there are explanation comments for each line
The line ‘connection, address = my_socket.accept()’ accepts TCP client connection and waits until it arrives. The send() method is used to transmit TCP message. This is enough for a beginner in network scripting. You will learn about the client in the next articles, “How to program” and “How to connect to the server”.
Like us on Facebook
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- Manage Databases And Hosted Servers Remotely With DbNinja : Database administration via command line is bit difficult for newbie system and database administrat...0 comments |
- Install Sayonara Player In Elementary OS / Ubuntu / Linux Mint : Sayonara is a small, clear and fast audio player for Linux written in C++, supported by the Qt frame...0 comments |
- How To Install Linux Kernel 3.12.4 In Ubuntu : Linux Kernel 3.12.4 has been officially released with fixes for inet6_init() cleanup order, seqlock ...0 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- Firefox 26 Has Been Released, How To Install It In Ubuntu And Its Derivates : Hi guys, Firefox 26 has been released for Windows, Mac OS X, Linux and Android devices. It brings m...0 comments |
- How To Install Linux Kernel 3.12.4 In Ubuntu
- Firefox 26 Has Been Released, How To Install It In Ubuntu And Its Derivates
- How To Install Brasero In Elementary OS ‘Luna’
- Install Sayonara Player In Elementary OS / Ubuntu / Linux Mint
- Manage Databases And Hosted Servers Remotely With DbNinja
- Install FrostWire 5.6.9 In Elementary OS ‘Luna’/ Ubuntu / Linux Mint
- Setup IT And Asset Management System With GLPI On Debian/Ubuntu
- How To Configure Linux Clients To Authenticate Using OpenLDAP
- How To Install Netflix In Ubuntu
- cowsay And fortune Combined Together
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .