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
We need your assistance to stay live
This week Top Posts
- Grive: An Unofficial, Open Source Linux Client For Google Drive : A couple of months before, we have featured a Linux client for Google Drive named Syncdrive, which w...2 comments |
- Wow! Linux Foundation To Offer Free Linux Course This Summer Worth $2,400 on EdX : Wow! Early last week it was all over the internet, if you haven't heard it yet then this is the t...2 comments |
- 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 |
- News from Kaspersky Lab: Tor is protecting criminals : In a previous article I wrote a bit about Tor. Free browser, relays bouncing your IP all over the ...1 comment |
- 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 |
- Install Sublime Text 3 In Fedora Easily With Fedy : Sublime Text is a sophisticated text editor for code, markup and prose. In this tutorial we are goi...0 comments |
- Adding and Deleting Cluster Resources (corosync pacemaker)
- News from Kaspersky Lab: Tor is protecting criminals
- Install Satis On Ubuntu Sever 13.10/13.04
- How To Access A LVM Drive / Partition in Ubuntu
- How To Disable Remember Open Files in Sublime Text 3
- Install Sublime Text 3 In Fedora Easily With Fedy
- Grive: An Unofficial, Open Source Linux Client For Google Drive
- Install nSnake Game In Terminal
- How To “Fix E: Could not open file /var/lib/dpkg/status”
- Wow! Linux Foundation To Offer Free Linux Course This Summer Worth $2,400 on EdX
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .