Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Connecting to a web server in Python 3

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading