Python - An HTTP Request in Python

Hello, I need help identifying what I´m doing wrong.

I´m trying to follow the lesson “HTTP Request in Python” by finding the file romeo.txt in VSCode using Python. I use the same code as the lesson

import socket
mysock = socket.socket((socket.AF_INET, socket.SOCK_STREAM))
mysock.connect(("data.py4e.com", 80))
cmd = "GET data.py4e.com/romeo.txt HTTP/1.0\n\n".encode()
mysock.send(cmd)

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

but I receive the following Traceback:

Traceback (most recent call last):
  File "c:\Users\Admin\Desktop\PY4E\fcc\sock.py", line 2, in <module>
    mysock = socket.socket((socket.AF_INET, socket.SOCK_STREAM))
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\socket.py", line 232, in __init__
    _socket.socket.__init__(self, family, type, proto, fileno)
TypeError: 'tuple' object cannot be interpreted as an integer

What am I doing wrong? If I run the progra, Ishould get back romeo.txt in my cli right?

What is url of lesson?

Url

Look carefully at this line. You are missing the protocol at the beginning of the url.

1 Like

The type error is coming from this line

mysock = socket.socket((socket.AF_INET, socket.SOCK_STREAM))

Should not be a tuple

Sorry about that. What I pointed out is also an error. But yes the first error is on the line you just referenced. Look carefully at the number of parentheses you are using. Should you be passing a tuple to socket.socket()?

1 Like