Python Socket - Network Programming Tutorial
Python Socket - Network Programming Tutorial
tutorial
Python By Silver Moon On Jul 22, 2012 112 Comments
To summarise the basics, sockets are the fundamental "things" behind any kind of
network communications done by your computer. For example when you type
www.google.com in your web browser, it opens a socket and connects to google.com to
fetch the page and show it to you. Same with any chat client like gtalk or skype. Any
network communication goes through a socket.
In this tutorial we shall be programming tcp sockets in python. You can also program
udp sockets in python.
This tutorial assumes that you already have a basic knowledge of python.
Creating a socket
This first thing to do is create a socket. The socket.socket function does this.
Quick Example :
Function socket.socket creates a socket and returns a socket descriptor which can be
used in other socket related functions
The above code will create a socket with the following properties ...
If any of the socket functions fail then python throws an exception called socket.error
which must be caught.
1
2
3 #handling errors in python socket programs
4
5 import socket #for sockets
6 import sys #for exit
7
try:
8
#create an AF_INET, STREAM socket (TCP)
9 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1 except socket.error, msg:
0 print 'Failed to create socket. Error code: ' + str(msg[0]) + ' ,
1 Error message : ' + msg[1]
1 sys.exit();
1
print 'Socket Created'
2
1
3
Ok , so you have created a socket successfully. But what next ? Next we shall try to
connect to some server using this socket. We can connect to www.google.com
Note
Connect to a Server
Before connecting to a remote host, its ip address is needed. In python the getting the ip
address is quite simple.
Now that we have the ip address of the remote host/system, we can connect to ip on a
certain 'port' using the connect function.
Quick example
$ python client.py
Socket Created
Ip address of www.google.com is 74.125.236.83
Socket Connected to www.google.com on ip 74.125.236.83
It creates a socket and then connects. Try connecting to a port different from port 80 and
you should not be able to connect which indicates that the port is not open for
connection. This logic can be used to build a port scanner.
OK, so we are now connected. Lets do the next thing , sending some data to the remote
server.
Free Tip
Other sockets like UDP , ICMP , ARP dont have a concept of "connection". These are
non-connection based communication. Which means you keep sending or receiving
packets from anybody and everybody.
Sending Data
In the above example , we first connect to an ip address and then send the string
message "GET / HTTP/1.1\r\n\r\n" to it. The message is actually an "http command" to
fetch the mainpage of a website.
Now that we have send some data , its time to receive a reply from the server. So lets do
it.
Receiving Data
Function recv is used to receive data on a socket. In the following example we shall
send the same message as the last example and receive a reply from the server.
$ python client.py
Socket Created
Ip address of www.google.com is 74.125.236.81
Socket Connected to www.google.com on ip 74.125.236.81
Message send successfully
HTTP/1.1 302 Found
Location: http://www.google.co.in/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=www.google.com
Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=www.google.com
Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=www.google.com
Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=.www.google.com
Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=.www.google.com
Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=.www.google.com
Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=google.com
Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=google.com
Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=google.com
Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=.google.com
Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=.google.com
Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/;
domain=.google.com
Set-Cookie:
PREF=ID=51f26964398d27b0:FF=0:TM=1343026094:LM=1343026094:S=pa0PqX9FCP
vyhBHJ; expires=Wed, 23-Jul-2014 06:48:14 GMT; path=/;
domain=.google.com
Google.com replied with the content of the page we requested. Quite simple!
Now that we have received our reply, its time to close the socket.
Close socket
1s.close()
Thats it.
Lets Revise
So in the above example we learned how to :
1. Create a socket
2. Connect to remote server
3. Send some data
4. Receive a reply
Its useful to know that your web browser also does the same thing when you open
www.google.com
This kind of socket activity represents a CLIENT. A client is a system that connects to
a remote system to fetch data.
The other kind of socket activity is called a SERVER. A server is a system that uses
sockets to receive incoming connections and provide them with data. It is just the
opposite of Client. So www.google.com is a server and your web browser is a client. Or
more technically www.google.com is a HTTP Server and your web browser is an HTTP
client.
1. Open a socket
2. Bind to a address(and port).
3. Listen for incoming connections.
4. Accept connections
5. Read/Send
We have already learnt how to open a socket. So the next thing would be to bind it.
Bind a socket
Function bind can be used to bind a socket to a particular address and port. It needs a
sockaddr_in structure similar to connect function.
Quick example
1 import socket
import sys
2
3
HOST = '' # Symbolic name meaning all available interfaces
4 PORT = 8888 # Arbitrary non-privileged port
5
6 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7 print 'Socket created'
8
9 try:
s.bind((HOST, PORT))
1 except socket.error , msg:
0 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' +
1
1
1
2
1 msg[1]
3 sys.exit()
1
4 print 'Socket bind complete'
1
5
1
6
Now that bind is done, its time to make the socket listen to connections. We bind a
socket to a particular IP address and a certain port number. By doing this we ensure that
all incoming data which is directed towards this port number is received by this
application.
This makes it obvious that you cannot have 2 sockets bound to the same port. There are
exceptions to this rule but we shall look into that in some other article.
After binding a socket to a port the next thing we need to do is listen for connections.
For this we need to put the socket in listening mode. Function socket_listen is used
to put the socket in listening mode. Just add the following line after bind.
1s.listen(10)
2print 'Socket now listening'
The parameter of the function listen is called backlog. It controls the number of
incoming connections that are kept "waiting" if the program is already busy. So by
specifying 10, it means that if 10 connections are already waiting to be processed, then
the 11th connection request shall be rejected. This will be more clear after checking
socket_accept.
Accept connection
1 import socket
import sys
2
3
HOST = '' # Symbolic name meaning all available interfaces
4 PORT = 8888 # Arbitrary non-privileged port
5
6 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7 print 'Socket created'
8
9
1
0
1
1
1
2
1
3
1 try:
4 s.bind((HOST, PORT))
except socket.error , msg:
1 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' +
5 msg[1]
1 sys.exit()
6
1 print 'Socket bind complete'
7
1 s.listen(10)
print 'Socket now listening'
8
1 #wait to accept a connection - blocking call
9 conn, addr = s.accept()
2
0 #display client information
2 print 'Connected with ' + addr[0] + ':' + str(addr[1])
1
2
2
2
3
2
4
2
5
Output
$ python server.py
Socket created
Socket bind complete
Socket now listening
So now this program is waiting for incoming connections on port 8888. Dont close this
program , keep it running.
Now a client can connect to it on this port. We shall use the telnet client for testing this.
Open a terminal and type
$ python server.py
Socket created
Socket bind complete
Socket now listening
Connected with 127.0.0.1:59954
So we can see that the client connected to the server. Try the above steps till you get it
working perfect.
We accepted an incoming connection but closed it immediately. This was not very
productive. There are lots of things that can be done after an incoming connection is
established. Afterall the connection was established for the purpose of communication.
So lets reply to the client.
Function sendall can be used to send something to the socket of the incoming
connection and the client should see it. Here is an example :
1 import socket
import sys
2
3
HOST = '' # Symbolic name meaning all available interfaces
4 PORT = 8888 # Arbitrary non-privileged port
5
6 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7 print 'Socket created'
8
9 try:
s.bind((HOST, PORT))
1 except socket.error , msg:
0 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' +
1 msg[1]
1 sys.exit()
1
2 print 'Socket bind complete'
1
s.listen(10)
3 print 'Socket now listening'
1
4 #wait to accept a connection - blocking call
1 conn, addr = s.accept()
5
1 print 'Connected with ' + addr[0] + ':' + str(addr[1])
6
#now keep talking with the client
1
data = conn.recv(1024)
7 conn.sendall(data)
1
8 conn.close()
1
9
2
0
2
1
2
2
2
3
2
4
2 s.close()
5
2
6
2
7
2
8
2
9
3
0
3
1
Run the above code in 1 terminal. And connect to this server using telnet from another
terminal and you should see this :
We can see that the connection is closed immediately after that simply because the
server program ends after accepting and sending reply. A server like www.google.com
is always up to accept incoming connections.
It means that a server is supposed to be running all the time. Afterall its a server meant
to serve. So we need to keep our server RUNNING non-stop. The simplest way to do
this is to put the accept in a loop so that it can receive incoming connections all the
time.
Live Server
So a live server will be alive always. Lets code this up
1 import socket
import sys
2
3
HOST = '' # Symbolic name meaning all available interfaces
4 PORT = 5000 # Arbitrary non-privileged port
5
6 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7 print 'Socket created'
8
9 try:
s.bind((HOST, PORT))
1 except socket.error , msg:
0 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' +
1 msg[1]
1 sys.exit()
1
2 print 'Socket bind complete'
1
s.listen(10)
3 print 'Socket now listening'
1
4 #now keep talking with the client
1 while 1:
5 #wait to accept a connection - blocking call
conn, addr = s.accept()
1
print 'Connected with ' + addr[0] + ':' + str(addr[1])
6
1 data = conn.recv(1024)
7 reply = 'OK...' + data
1 if not data:
8 break
1
conn.sendall(reply)
9
2
conn.close()
0 s.close()
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
Now run the server program in 1 terminal , and open 3 other terminals.
From each of the 3 terminal do a telnet to the server port.
$ python server.py
Socket created
Socket bind complete
Socket now listening
Connected with 127.0.0.1:60225
Connected with 127.0.0.1:60237
Connected with 127.0.0.1:60239
So now the server is running nonstop and the telnet terminals are also connected
nonstop. Now close the server program. All telnet terminals would show "Connection
closed by foreign host."
Good so far. But still there is not effective communication between the server and the
client. The server program accepts connections in a loop and just send them a reply,
after that it does nothing with them. Also it is not able to handle more than 1 connection
at a time. So now its time to handle the connections , and handle multiple connections
together.
Handling Connections
To handle every connection we need a separate handling code to run along with the
main server accepting connections. One way to achieve this is using threads. The main
server program accepts a connection and creates a new thread to handle communication
for the connection, and then the server goes back to accept more connections.
We shall now use threads to create handlers for each connection the server accepts.
1 import socket
import sys
2
from thread import *
3
4 HOST = '' # Symbolic name meaning all available interfaces
5 PORT = 8888 # Arbitrary non-privileged port
6
7 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8 print 'Socket created'
9
#Bind socket to local host and port
1 try:
0 s.bind((HOST, PORT))
1 except socket.error , msg:
1 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' +
1 msg[1]
sys.exit()
2
1 print 'Socket bind complete'
3
1 #Start listening on socket
4 s.listen(10)
1 print 'Socket now listening'
5
#Function for handling connections. This will be used to create
1
threads
6 def clientthread(conn):
1 #Sending message to connected client
7 conn.send('Welcome to the server. Type something and hit enter\
1 n') #send only takes string
8
#infinite loop so that function do not terminate and thread do
1 not end.
9 while True:
2
0 #Receiving from client
2 data = conn.recv(1024)
1 reply = 'OK...' + data
if not data:
2 break
2
2 conn.sendall(reply)
3
2 #came out of loop
4 conn.close()
2
5 #now keep talking with the client
while 1:
2 #wait to accept a connection - blocking call
6 conn, addr = s.accept()
2 print 'Connected with ' + addr[0] + ':' + str(addr[1])
7
2 #start new thread takes 1st argument as a function name to be
run, second is the tuple of arguments to the function.
8 start_new_thread(clientthread ,(conn,))
2
9 s.close()
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
Run the above server and open 3 terminals like before. Now the server will create a
thread for each client connecting to it.
$ python server.py
Socket created
Socket bind complete
Socket now listening
Connected with 127.0.0.1:60730
Connected with 127.0.0.1:60731
The above connection handler takes some input from the client and replies back with the
same.
Conclusion
By now you must have learned the basics of socket programming in python. You can
try out some experiments like writing a chat client or something similar.
When it comes up, simply change the port number and the server would run fine.
If you think that the tutorial needs some addons or improvements or any of the code
snippets above dont work then feel free to make a comment below so that it gets fixed.