variable as byte - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: variable as byte (/thread-22579.html) |
variable as byte - jesshove - Nov-18-2019 I have been learning Python from 'Python for Everybody' by Charles Severance and found it very good However, I have used his suggested code for downloading an image from a webpage and though I have checked it a few times and tried some alternatives, I always get a Traceback message The ocde is copied below. Could some kind person please spot the mistake! Not sure if it's me or a misprint of courseMany thanks! Jessica import socket import time HOST='data.pr4e.org' PORT = 80 mysock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect((HOST,PORT)) mysock.sendall(b'GET http://data.pr4e.org/cover3.jpg HTTP/1.0\r\n\r\n') count = 0 picture = b"" while True: data = mysock.recv(5120) if len(data)<1: break #time.sleep(0.25) count = count + len(data) print(len(data),count) picture = picture + data mysock.close() #look for end of header pos = picture.find(b"\r\n\r\n") print("Header length",pos) print(picture[:pos].decode()) #skip header and save picture data picture = picture[pos+4] fhand = open("stuff.jpg",'wb') print("got this far") fhand.write(picture) fhand.close() |