Python

 

 

 

 

Python - Socket

 

Python socket programming is a method of creating network applications using the built-in socket module in Python. The socket module provides a way to create, manage, and use sockets, which are low-level network communication endpoints. Using sockets, you can create applications that send and receive data over various network protocols, such as TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).

 

Here's a detailed explanation of Python socket programming concepts:

 

Socket: A socket is an endpoint for sending and receiving data across a network. In Python, you create a socket using the socket.socket() function, which returns a socket object. When creating a socket, you need to specify the address family (e.g., socket.AF_INET for IPv4) and the socket type (e.g., socket.SOCK_STREAM for TCP or socket.SOCK_DGRAM for UDP).

 

Binding: Binding refers to the process of associating a socket with a specific IP address and port number on the local machine. This is done using the bind() method of a socket object, which takes a tuple containing the IP address and port number as its argument. Binding is required for server sockets, as they need to listen for incoming connections on a specific address and port.

 

Listening: For server sockets using TCP, you need to start listening for incoming connections after binding. This is done using the listen() method of a socket object. The method takes an integer argument, which specifies the maximum number of queued connections that can be waiting to be accepted by the server.

 

Accepting: After a server socket starts listening, it can accept incoming connections using the accept() method. This method blocks until a connection is received, and it returns a new socket object and the client's address when a connection is established. The new socket object is used for communication with the connected client, while the original server socket continues to listen for new connections.

 

Connecting: For client sockets, you need to initiate a connection to a server using the connect() method of a socket object. This method takes a tuple containing the server's IP address and port number as its argument. Once the connection is established, the client socket can send and receive data using the same socket object.

 

Sending and Receiving: To send data over a socket, you can use the send() or sendall() methods for TCP sockets, and the sendto() method for UDP sockets. Data sent over a socket should be in bytes format. To receive data, you can use the recv() method for TCP sockets, and the recvfrom() method for UDP sockets. These methods return the received data in bytes format, which can be converted to a desired data type, such as a string, using the decode() method.

 

Closing: After the communication is complete, it's essential to close the socket using the close() method of a socket object. This releases the resources associated with the socket and allows the operating system to reuse the underlying network connection.

 

 

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

 

 

RAW Packet

 

< Sender >

 

s = socket.socket(socket.AF_INET, socket.SOCK_RAW,protocol_number)

....

s.sendto(UserData, (dstination_IP, 0))

 

 

< Reciever >

 

s = socket.socket(socket.AF_INET, socket.SOCK_RAW,protocol_number)

s.bind((IP_Reciever,0))

....

recvBytes = s.recv(NumberOfBytes)

 

 

Examples >