selfteaching / How-To-Ask-Questions-The-Smart-Way

568 stars 290 forks source link

PY4E Chapter 12 --please help #18

Open alwaysasking opened 3 years ago

alwaysasking commented 3 years ago

I'm on Ch.12 (video C--write a web browser)

I'm testing the HTTP Request on my Atom application / command prompt, and in Python itself.

I copied the code exactly as shown in the video:

import socket

mysock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) mysock=connect(('data.pr4e.org',80)) cmd='GET hhtp://www.pr4e.org/romeo.txt' HTTP/1.0\n\n.encode() mysock.send(cmd)

while True: data=myscok.recv(512) if (len(data)<1): break print(data.decode()) mysock.close()

when I run the above in command prompt, and in Python, I get a SyntaxError (as shown below)

Command Prompt: File "C:\Users\fetyl\Desktop\py4e\12.C.py", line 5 cmd='GET hhtp://www.pr4e.org/romeo.txt' HTTP/1.0\n\n.encode() ^ SyntaxError: invalid syntax

Python: Traceback (most recent call last): File "", line 1, in NameError: name 'connect' is not defined

cmd='GET hhtp://www.pr4e.org/romeo.txt' HTTP/1.0\n\n.encode() File "", line 1 cmd='GET hhtp://www.pr4e.org/romeo.txt' HTTP/1.0\n\n.encode() ^ SyntaxError: invalid syntax mysock.send(cmd) Traceback (most recent call last): File "", line 1, in NameError: name 'cmd' is not defined

while True: ... data=myscok.recv(512) ... if (len(data)<1): ... break ... print(data.decode()) ... mysock.close() File "", line 6 mysock.close() ^ SyntaxError: invalid syntax

I'm not sure what I've done wrong. I put a single space, as directed, and typed everything word-for-word from the video. I even went to the slide version and copy-and-pasted to make sure I wasn't falling victim to human error (ascertaining that my eyes weren't tricking me into thinking it was exactly the same).

Please help. What the f*** am I doing wrong?

alwaysasking commented 3 years ago

UPDATE:

I put "mysock=connect" instead of "mysock.connect", I didn't have the "\r"s, and my ' ' weren't positioned correctly (if someone can please tell me why the \r characters are needed, I would much appreciate the insight.

the below code works with no SyntaxErrors

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect(('data.pr4e.org', 80)) cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode() mysock.send(cmd)

while True: data = mysock.recv(512) if len(data) < 1: break print(data.decode(),end='') mysock.close()