3. Network Programming
We Use Sockets for Network Programming :
TCP and UDP Sockets
Regular Servers and Clients
Raw Sockets
Sniffing and Injection
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
5. Socket
A network socket is an endpoint of an inter-process communication
flow across a computer network.
The Python socket module provides direct access to the standard BSD
socket interface, which is available on most modern computer
systems. The advantage of using Python for socket programming is
that socket addressing is simpler and much of the buffer allocation is
done for you. In addition, it is easy to create secure sockets and
several higher-level socket abstractions are available.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
6. Socket
To create a server, you need to:
create a socket
bind the socket to an address and port
listen for incoming connections
wait for clients
accept a client
send and receive data
To create a client, you need to:
create a socket
connect to the server
send and receive data
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
7. Address
The BSD socket interface defines several different types of addresses called
families. These include:
AF_UNIX: A Unix socket allows two processes running on the same machine
to communicate with each other through the socket interface. In Python,
UNIX socket addresses are represented as a string.
AF_INET: An IPv4 socket is a socket between two processes, potentially
running on different machines, using the current version of IP (IP version 4).
This is the type of socket most programs use today. In Python, IPv4 socket
addresses are represented using a tuple of (host, port), where host is a string
host name and port is an integer called the port number. For the host name
you can specify either a standard Internet name, such as 'www.cnn.com' or an
IP address in dotted decimal notation, such as '64.236.24.20'.
AF_INET6: An IPv6 socket is similar to an IPv4 socket, except that it uses IP
version 6
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
8. Address
To create a socket in Python, use the socket() method:
socket(family,type[,protocol])
The family is either AF_UNIX, AF_INET, or AF_INET6. There are several
different types of sockets; the main ones are SOCK_STREAM for TCP sockets
and SOCK_DGRAM for UDP sockets. You can skip the protocol number in
most cases.
Please note that the constants listed above for socket family and socket type
are defined inside the socket module. This means that you must import the
socket module and then reference them as 'socket.AF_INET'.
The important thing about the socket() method is it returns a socket object.
You can then use the socket object to call each of its methods, such as bind,
listen, accept, and connect.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
9. import socket
#creating TCP Socket listen on a port
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# We bind Socket to 8000 Port and Interface with IP 0.0.0.0
tcp_socket.bind(('0.0.0.0', 8000))
# Start listening on IP and PORT for CLIENTS
tcp_socket.listen(2) # Argument here 2 is Number of Concurrent client Socket
can handle
# Start Accepting Clients
(client, (ip, port)) = tcp_socket.accept()
print client
print ip
print port
client.send('Welcome to PYSEC101 Course')
data = client.recv(2048) #buffer size is 2048
print data
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
10. -----Client----
nc 127.0.0.1 8000
Welcome to PYSEC101 Course
-----Server-----
<socket._socketobject object at 0x1420590>
127.0.0.1
33821
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
11. Echo Server
import socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.bind(('127.0.0.1', 8000))
tcp_socket.listen(2)
print 'Waiting for client ...'
(client, (ip, port)) = tcp_socket.accept()
print 'Revived connection from : ', ip
print 'Starting ECHO output...'
data = 'dummy'
while len(data):
data = client.recv(2048)
print 'Client send : ', data
client.send(data)
print 'Closing Connection'
client.close()
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
12. -----Client----
nc 127.0.0.1 8000
Salam
Salam
PYSEC101 Echo Server
PYSEC101 Echo Server
^C
-----Server-----
Waiting for client ...
Revived connection from : 127.0.0.1
Starting ECHO output...
Client send : Salam
Client send : PYSEC101 Echo Server
Client send :
Closing Connection
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
13. Echo Server With Reuse Address
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
import socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcp_socket.bind(('127.0.0.1', 8000))
tcp_socket.listen(2)
print 'Waiting for client ...'
(client, (ip, port)) = tcp_socket.accept()
print 'Revived connection from : ', ip
print 'Starting ECHO output...'
data = 'dummy'
while len(data):
data = client.recv(2048)
print 'Client send : ', data
client.send(data)
print 'Closing Connection'
client.close()
14. Process Client Options
When client comes Connect we need to process client
Process client Sequentially and one at a time
Multi_Threaded Server
Multi_Process Server
Non_Blocking Sockets with Select (Multiplexing)
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
15. Exercise
Create a simple Echo Server to handle 1 client
Create a Multi_Threaded Echo Server
Create a Multi_Process Echo Server
Create a Non-Blocking Multiplexed Echo server using select()
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
16. This work is licensed under the Creative Commons
Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/
Copyright 2013 Mohammad reza Kamalifard.
All rights reserved.