The problem (exercise 12.5):
Change the socket1.py program so that it only shows data
after the headers and a blank line have been received. Remember that recv is
receiving characters (newlines and all) - not lines.-
socket1.py
My submission:
Change the socket1.py program so that it only shows data
after the headers and a blank line have been received. Remember that recv is
receiving characters (newlines and all) - not lines.-
socket1.py
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')
while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
print data;
mysock.close()
My submission:
import socket
import re
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')
while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
if(data.find('\r\n\r')!= -1):
prt = re.findall('\r\n\r([\w\s]*)',data)
print prt[0]
else: print data
mysock.close()
No comments:
Post a Comment