socket.gaierror: [Errno 11001] getaddrinfo failed

Asked

Viewed 910 times

-1

Good afternoon!

I am studying Python and came across a problem while studying about network (socket library). I have the following piece of code

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

mysock.connect(('http://data.pr4e.org/', 80))
cmd = 'GET http://data.pr4e.org/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())
mysock.close()

So I checked all the links work and are without problems, but when running the script an error is shown on the screen

Traceback (most recent call last):
  File "C:\Users\Andre\Desktop\PythonMichiganSchool\network\script.py", line 4, in <module>
    mysock.connect(('http://data.pr4e.org/', 80))
socket.gaierror: [Errno 11001] getaddrinfo failed

I tried to change the links and test them, rewrote the code and read the library documentation and even some answers here in stackoverflow, but I did not succeed in solving the problem

1 answer

0

To resolve this issue, you should remove HTTP:// from the following line

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

After this change the code ' n' to ' r n n n The code will look like the code below :

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())
mysock.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.