Write a TCP Server in Python. Introduction with Sockets – Part 1
It is late and I decided to code a simple TCP server in pure Python. But what is a server? Anything that receives connection from other computers called clients is called a server. This server keeps the connection open and waits for clients to connect. Python is rich in libraries and the socket library will help us to create the server. What is a socket? A socket is one end-point of a two-way
communication link between two programs running on the network. If you have previous experience with sockets in C programming language you understand very well what I am trying to explain. When you watch a movie online, a socket is created in the client side which connects to the server that does the streaming. Sockets are necessary and powerful. The base of the server we will learn to write is the socket and to create and use sockets in python we must import the socket library like shown in Figure 1. To create the server we need to create a socket, listen for incoming connections, accept clients when they try to connect and send and receive data. Once you create a socket object you can use functions to operate with it.
Figure 1 – Importing socket library
Since we need a constant connection between the client and the server we should create a socket_stream like shown in Figure 2.
Figure 2 – Creating the socket
Now it is time to use this socket. We should open a port where we want the client to connect the server. The socket we created is assigned to the socket_stream variable so now everytime we want to use the socketwe can refer to that variable. How do we put the ‘server’ in listening mode? We use the ‘listen()’ method to set up start TCP listener. This is the part one of our Python for Networking series. It is very easy to follow because we explain things step by step and don’t do much coding in this part. I prefer to take it slow so we can build strong skills and and I am sure that you guys will understand very well the next article of these series.
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 |
- How To Show Username On Panel In Ubuntu 13.04/13.10 : By default usernames are not displayed on panel in Ubuntu 13.04 and 13.10, In this simple tutorial w...0 comments |
- cowsay And fortune Combined Together : Hi linux geeks! In this article I will teach you a nice trick you can perform in the terminal with ...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 |
- Install lamp with 1 command in Ubuntu 12.10, 13.04 Raring Ringtail & LinuxMint13 : Updated: 10/09/2012 :LAMP (Linux, Apache, MySQL and PHP) is an open source Web development platform ...0 comments |
- Install and Configure Samba share in Ubuntu 13.10 'Saucy Salamander' , 13.04| Howto : Updated 05-04-2013: One of the most asked features for Samba is a graphical user interface to help w...0 comments |
- cowsay And fortune Combined Together
- How To Show Username On Panel In Ubuntu 13.04/13.10
- Setup DNS Server On openSUSE 13.1
- TeamViewer 9 Final Has Been Released!
- Twelve Vulnerabilities Have Been Fixed In Ubuntu 12.04, Time To Update
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6
- Install LibreOffice 4.1.3 in Elementary OS ‘Luna’
- How To Install Simple Scan in Crunchbang ‘Waldorf’
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .