[Tutor] Beginner: Socket object and packets

Alan Gauld alan.gauld at btinternet.com
Sat Dec 5 17:56:21 EST 2015


On 05/12/15 13:21, Marc Eymard 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.

You need to be careful with your descriptions. ping is a very
specific message and uses ICMP echo rather than TCP/IP and looks
nothing like http. In another context it would be OK but when
dealing with sockets you need to be precise about what you are
actually sending.

> My assumption is there will be a mandatory blank line right after the 
> http headers in accordance with the http protocol.
> 
> 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)


Is there any reason why you are using raw sockets rather than
the httplib module which does most of this stuff for you
(and probably has useful code you code study if you do want
to do it this way)?

> My question:
> 
> Why is the following statement False when there is an actual blank line 
> in the received packet:
>      '\n\n' in data

It looks like you are using Python v3. Remember that most
system level IO in v3 uses bytes not strings. You probably
need to convert the bytes to a string before looking for
blank lines. But that's just a guess.

Also did you try using find() or index() rather than in?

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list