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
We need your assistance to stay live
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 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 Server (Apache, MySQL or MariaDB, PHP) On Ubuntu 13.10 : Updated January 12, 2014 LAMP is a combination of operating system and open-source software stack. ...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 |
- DNS Server Installation Step by Step Using CentOS 6.5/6.4/6.3 : DNS, Domain Name System, translates hostnames or URLs into IP addresses. For example if we type www....0 comments |
- How to Install uTorrent Client on Ubuntu/Debian/Linux Mint : uTorrent is a lightweight and efficient BitTorrent client for Linux, Windows OS and Mac OS. The inst...0 comments |
- Fedora 21 Scheduled For Release In October
- Install Uget Download Manager In Ubuntu, Fedora, Debian
- Download Elementary OS ‘Luna’ Official Wallpapers
- Install Power Commands 0.1.5 On Ubuntu 14.04/13.10/12.10/12.04
- 5 Android Apps that’s Going to Make Running Your Business Less Stressful
- Puppet agent Install Using Script
- Install Corosync and Pacemaker On CentOS 6.5
- Install Fail2Ban On Ubuntu Server 13.04/13.10
- How To Install Google Chrome in Fedora 20, 19, 18
- How to surf using Tor Browser Bundle
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .