python retrieving web data -


i new @ python , have been trying figure out following exercise.

exercise 5: (advanced) change socket program shows data after headers , blank line have been received. remember recv receiving characters (newlines , all), not lines.

i attached below code came with, unfortunately don't think working:

import socket mysocket=socket.socket(socket.af_inet,socket.sock_stream) mysocket.connect(('data.pr4e.org', 80)) mysocket.send('get http://data.pr4e.org/romeo.txt http/1.0\r\n\r\n'.encode())  count=0 while true:           data = mysocket.recv(200)            if (len(data) < 1): break              count=count+len(data.decode().strip())           print(len(data),count)           if count >=399:                  print(data.decode(),end="")          mysocket.close() 

instead of counting number of lines received, grab data , split on first double crlf find.

resp = [] while true:           data = mysocket.recv(200)            if not data: break             resp.append(data.decode()) mysocket.close()  resp = "".join(resp) body = resp.partition('\r\n\r\n')[2] print(body) 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -