Socket Module and basic Networking with python 

This module provides access to the BSD socket interface. It is available on all modern Unix systems, Windows, MacOS, and probably additional platforms. The Python interface is a straightforward transliteration of the Unix system call and library interface for sockets to Python’s object-oriented style: the socket() function returns a socket object whose methods implement the various socket system calls. Parameter types are somewhat higher-level than in the C interface: as with read() and write() operations on Python files, buffer allocation on receive operations is automatic, and buffer length is implicit on send operations. Source

Before we start to write some basic Server/Client script lets take a quick look at some of the functions available in Socket module:

socket() -- create a new socket object

socketpair() -- create a pair of new socket objects

fromfd() -- create a socket object from an open file descriptor

fromshare() -- create a socket object from data received fromsocket.share

gethostname() -- return the current hostname

gethostname() -- return the current hostname

gethostbyname() -- map a hostname to its IP number

gethostbyaddr() -- map an IP number or hostname to DNS info

getservbyname() -- map a service name and a protocol name to a port number

getprotobyname() -- map a protocol name (e.g. 'tcp') to a number

ntohs(),ntohl() -- convert 16, 32 bit int from network to host byte order

htons(), htonl() -- convert 16, 32 bit int from host to network byte

order net_aton() -- convert IP addr string ( to 32-bit

packed format

net_ntoa() -- convert 32-bit packed format IP to string (

socket.getdefaulttimeout() -- get the default timeout value

socket.setdefaulttimeout() -- set the default timeout value



Now lets script two sockets and connect them. they can connect from two separate locations or within the same machine. for this tutorial we are going to create both sockets on the same machine(Local host) First thing first, we need to import the module and happily unlike Socket programming in C 'With all do respect' our job will be much easier, of cource we want to have some of the low level controls over our codes but we can get our code up and run in no time.

we need to provide ip or host name, it can be or any given ip.socket.gethostname()method gets the name of the local machine which is also we need to specify port number, here we usee 5000 and the only advantage of choosing it is the fact Operating system doesnt use it and its none of default ports, so you can use any port as long as it's not part of the default ports like 80, 145, 23 ... In the next line server s an object of socket class. there would be more parameters that we can use but for now lets keep it simple as possible.

now with bind() method we actually bind ip, port to the socket. basically our server is online "in our case localy" waiting for connection.

We create two variables connection and address they can be anything as naming goes but as always it's better to choose meaningful variable to makes our code easier to read. accept() method returns an open connection between the server and client, along with the address of the client. The connection is a different socket "we are going to script it". Now using while loop server constantly is listening for connection and now is possible to send and recieve data between the nodes.

Lets send some message Data. when we send message over nodes "sockets" we cany use string format we need to convert them to bytes first, so what we need to do is to encode the string and decode it in other end.

so we create a variable simply to hold the message. and by using send() and encode() method we change the string to bytes and send it to specific address:(ip,port,socket). by the way: UTF-8 is a variable width character encoding capable of encoding all 1,112,064 valid code points in Unicode using one to four 8-bit bytes. The encoding is defined by the Unicode Standard, and was originally designed by Ken Thompson and Rob Pike. The name is derived from Unicode Transformation Format – 8-bit wiki And the last thing we need to do is to close the connection. but as long as the while loop is alive the cycle will continue. so lets leave the server side and take care of the client so we can demonstrate the connection in action.


The client side shares alot of the same concepts and in our case it's very straight forward.

The different is that now we just get our socket to recieve something by connecting to the given ip/port. client.recv(1024) 1024 is a buffer size will recieve what ever is coming from server side. and smply print it out. to test our two sockets first run the server socket and then run client socket. As out put we should get the message that sent by server. of course this was very short introduction on scket programming in python. as always there are a lot more in to it specially in this subject which have deep connection to TCP/IP and netwroeking so extra knowledge on Networking is really a plus. so i incurage you to take your time and take a look at python socket module here

If you're interested to have a fast general overview on TCP/IP check out the link below: ITPRC

and finally check out my more indept tutorials for socket programming and remote connection with python: