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?

1 Like

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

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.