Python : How to read details of a request at Packet level

Viewed 295

Greetings folks of stackoverflow!

I am writing a python webserver using sockets to concrete my understanding with how HTTP requests works. So far, i am able to capture a HTTP request over an open socket. The code goes something like this:

import socket
from time import sleep
c = None #Client socket1
addr = None #Client address1
    
server_socket1 = socket.socket() #by default it is SOCK_STREAM (TCP) and has porotocal AF_INET (IPv4) 

server_socket1.bind(('127.0.0.1',9999)) #server machine's ip and port on which it will send and recieve connections from

server_socket1.listen(2) #We will only accept two connections as of now , one for each client
print("Server started successfully!!!")
print("Waiting for connections...\n\n")

while (((c is None)and(addr is None))):
    if((c is None) and (addr is None)):
        c,addr = server_socket1.accept()
        print("User connected to client1 socket!!")
        print("Client connected ip address "+str(addr))
        

        
while True:
    msg = c.recv(4096)
    if(msg!=None):
        #print(msg)
        headers, sep, body = msg.partition(b'\r\n\r\n')
        headers = headers.decode('utf-8') 
        print(headers)

        html_body = "<html><body><h1>This is a test</h1><p>More content here</p></body></html>"
        response_headers = {
        'Content-Type': 'text/html; encoding=utf8',
        'Content-Length': len(html_body),
        'Connection': 'close',
        }

        response_headers_raw = ''.join('%s: %s\r\n' % (k, v) for k, v in response_headers.items())
        response_proto = 'HTTP/1.1'
        response_status = '200'
        response_status_text = 'OK' # this can be random

        # sending all this stuff
        r = '%s %s %s\r\n' % (response_proto, response_status, response_status_text)
        c.sendall(r.encode())
        c.sendall(response_headers_raw.encode())
        c.sendall(b'\r\n') # to separate headers from body
        c.send(html_body.encode(encoding="utf-8"))

        sleep(5)

The output :

When the server starts

Sending a request from browser to the open socket:

enter image description here

The captured/incoming request reflected on the browser:

enter image description here

Now, what I want to do is to be able to identify the TCP information of the packets, i.e. the flags. Is there any way I can view that using the mentioned implementation??

Any leads would be much appreciated.

~Regards

1 Answers

By the time your TCP socket yields the data, the TCP module in the kernel has unsegmented the data, and the individual packets have been discarded — only the carried byte stream has been retained.

In order to capture the individual packets, you need to hook into the networking stack at a much lower level. The common way is to use a raw IP socket, either directly, or through the libpcap library, or through one of the tcpdump or wireshark utilities.

Related