0

I am trying to connect to a web server in Python 3 and it just doesn't work! I wrote the following in my code editor VS Code and btw, I don't have telnet installed. So, here's my code:

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('http://data.pr4e.org',80))

And the traceback I am getting:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.gaierror: [Errno 11001] getaddrinfo failed

Any suggestions?

2
  • Are you doing this at such a low level on purpose? There are built-in and third-party modules that speak HTTP. Commented May 7, 2022 at 15:38
  • In any case, I don't think you should be including http://. Try just data.pr4e.org. Commented May 7, 2022 at 15:40

1 Answer 1

1

The error is saying name lookup fails, and for good reason. When using raw sockets, you mustn't put the http:// protocol in the hostname string.

mysock.connect(('http://data.pr4e.org', 80))

must be

mysock.connect(('data.pr4e.org', 80))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.