[Tutor] Beginner: Socket object and packets
Cameron Simpson
cs at zip.com.au
Sat Dec 5 20:47:15 EST 2015
On 05Dec2015 13:21, Marc Eymard <marc_eymard at hotmail.com> wrote:
>Hi tutor,
>I am trying to locate the first blank line in the first received packet when
>pinging an internet server using a socket object.
First up: everything ALan already said.
Next:
Note that the HTTP response need not all be in a single packet, though that is
not your problem.
>My assumption is there will be a mandatory blank line right after the
>http headers in accordance with the http protocol.
There certainly should be.
>Consider the following:
>import socket
>mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>mysock.connect( ('www.py4inf.com/code/', 80) )
>mysock.send('GET http://www.py4inf.com/code/' + ' HTTP/1.0\n\n')
>data_str = mysock.recv(700)
>
>My question:
>
>Why is the following statement False when there is an actual blank
>line in the received packet:
> '\n\n' in data
1: You will be getting bytes backup from the server (obviously so in Python 3
and implicitly in Python 2).
2: The HTTP protocol, like most internet text protocols, ends lines with the
bytes '\r\n', not just '\n'.
Therefore you should expect the bytes '\r\n\r\n' in the response data.
However, you should have discovered this already by doing some debugging. Since
you're clearly not getting the response you expected, the very first step on
your part should be to print our the received data, for example by adding:
print(repr(data_str))
after your recv() call. Then you could inspect the received data and probably
have seen the '\r' characters.
Cheers,
Cameron Simpson <cs at zip.com.au>
More information about the Tutor
mailing list