Search code examples
pythonsocketstcphttp-request

On which bit does the Data field start in a TCP segment?


I want to read HTTP requests using sockets on Python. Specifically, the Request Line, General Headers, and Request Headers, based on this. To perform this, I need to read the Data field of the TCP segment, but how will I know on which bit the Data field starts on? It would be on a multiple of 32, but would I have to brute force it (i.e. checking if the first 32 bytes look like "GET /index.html HTTP/1.1").


Solution

  • With a stream socket, Python takes care of the TCP encapsulation/decapsulation for you. Thus, all you have to do is use the socket recv function - the first bytes you receive will be GET (assuming a well-formed HTTP request).