socket.connect() gives socket.gaierror: [Errno 11004] getaddrinfo failed

A. Drosi

I have already checked answers regarding my problem, but I couldn't find what's wrong. I am new to Python and that might be a problem. I have written this simple code to connect to a site, but I get this error:

socket.gaierror: [Errno 11004] getaddrinfo failed

This is my code:

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('http://www.py4e.com', 80))
mysock.send('GET http://www.py4e.com/code3/mbox-short.txt HTTP/1.0\n\n')
while True:
    data = mysock.recv(512)        
    if(len(data) < 1):
        break
    print (data)
mysock.close()
Torxed
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4e.com', 80))
mysock.send('GET http://www.py4e.com/code3/mbox-short.txt HTTP/1.0\n\n')
while True:
    data = mysock.recv(512)        
    if(len(data) < 1):
        break
    print (data)
mysock.close()

Quite simple, don't use http:// in your host declaration on .connect().
http:// is a protocol and www.py4e.com is a host (or A record in a DNS server). The standard socket library doesn't know anything regarding protocols and there for requires only a host and a port number.
If you want automated processes check out urllib.request or @Mego's answer using Requests which handles the connection and HTTP parsing for you.

Also if you're using Python3 which you probably should, you need to send bytes data when doing .send().

There's two ways of converting your string to bytes data:

mysock.send(b'GET http://www.py4e.com/code3/mbox-short.txt HTTP/1.0\n\n')
mysock.send(bytes('GET http://www.py4e.com/code3/mbox-short.txt HTTP/1.0\n\n', 'UTF-8'))

Both does the same thing basically.

Finally, in a GET request you don't request http:// either.
Instead you just send the path to the file you want to retrieve:

mysock.send(b'GET /code3/mbox-short.txt HTTP/1.0\n\n')

The reason is (again) that http:// is a protocol descriptor and not part of the actual protocol data being sent. You also don't need the host declaration in your GET request because the server that you connected to already knows which host you're on - since you're... connected to it.
Instead the server expects you to supply a Host: <hostname>\r\n header if the host is serving multiple virtual hosts.
You might need a few other headers tho to be able to request actual content from certain web-servers.

But this is the basic jist of things.

Continue reading

Here's a good start:

It shows you what a raw GET request looks like.
An in the future I recommend using your browsers built-in Network Debugger which can show raw headers, raw responses and a whole bunch of other things.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

jupyter throwing error: socket.gaierror: [Errno -2] Name or service not known

From Dev

socket.gaierror: [Errno -2] Name or service not known

From Dev

urlopen error [Errno 11001] getaddrinfo failed

From Dev

(python) [Errno 11001] getaddrinfo failed

From Dev

Server connection issue: "socket.gaierror: [Errno 11004] getaddrinfo failed"

From Dev

socket connect() returns errno EINVAL

From Dev

Python Socket gives "[Errno 24] Too many open files"

From Dev

socket.error: [Errno 111] when trying to connect to a socket

From Dev

OpenSSL: socket: Connection refused connect:errno=111

From Dev

Pycharm python console socket.gaierror

From Dev

socket.gaierror: [Errno 8] nodename nor servname provided, or not known

From Dev

Tornado Python blocking on _socket.getaddrinfo

From Dev

socket.gaierror when trying to run emr using python mrjob

From Dev

Netty connect to unix domain socket failed

From Dev

gaierror: [Errno 11004] getaddrinfo failed

From Dev

urlopen error [Errno 11001] getaddrinfo failed?

From Dev

Socket issue, client claiming it failed to connect, but server says it did

From Dev

Why am I getting socket.gaierror with httplib?

From Dev

Failed to create datagram socket (OS Error: Permission denied, errno = 13

From Dev

jupyter throwing error: socket.gaierror: [Errno -2] Name or service not known

From Dev

Server Socket errno 57 - Socket is not connected

From Dev

(python) [Errno 11001] getaddrinfo failed

From Dev

Python ex_setup.py urlopen error [Errno 11004] getaddrinfo failed

From Dev

socket.error: [Errno 111] when trying to connect to a socket

From Dev

Python - Telnetlib socket.gaierror:

From Dev

Failed to connect to Mir: Failed to connect to server socket: No such file or directory

From Dev

getaddrinfo failed with socket.gaierror[11001] (python) (mqtt)

From Dev

socket.gaierror: Errno 11004 getaddrinfo failed

From Dev

connect to socket failed - errno 88 (cpp)

Related Related

  1. 1

    jupyter throwing error: socket.gaierror: [Errno -2] Name or service not known

  2. 2

    socket.gaierror: [Errno -2] Name or service not known

  3. 3

    urlopen error [Errno 11001] getaddrinfo failed

  4. 4

    (python) [Errno 11001] getaddrinfo failed

  5. 5

    Server connection issue: "socket.gaierror: [Errno 11004] getaddrinfo failed"

  6. 6

    socket connect() returns errno EINVAL

  7. 7

    Python Socket gives "[Errno 24] Too many open files"

  8. 8

    socket.error: [Errno 111] when trying to connect to a socket

  9. 9

    OpenSSL: socket: Connection refused connect:errno=111

  10. 10

    Pycharm python console socket.gaierror

  11. 11

    socket.gaierror: [Errno 8] nodename nor servname provided, or not known

  12. 12

    Tornado Python blocking on _socket.getaddrinfo

  13. 13

    socket.gaierror when trying to run emr using python mrjob

  14. 14

    Netty connect to unix domain socket failed

  15. 15

    gaierror: [Errno 11004] getaddrinfo failed

  16. 16

    urlopen error [Errno 11001] getaddrinfo failed?

  17. 17

    Socket issue, client claiming it failed to connect, but server says it did

  18. 18

    Why am I getting socket.gaierror with httplib?

  19. 19

    Failed to create datagram socket (OS Error: Permission denied, errno = 13

  20. 20

    jupyter throwing error: socket.gaierror: [Errno -2] Name or service not known

  21. 21

    Server Socket errno 57 - Socket is not connected

  22. 22

    (python) [Errno 11001] getaddrinfo failed

  23. 23

    Python ex_setup.py urlopen error [Errno 11004] getaddrinfo failed

  24. 24

    socket.error: [Errno 111] when trying to connect to a socket

  25. 25

    Python - Telnetlib socket.gaierror:

  26. 26

    Failed to connect to Mir: Failed to connect to server socket: No such file or directory

  27. 27

    getaddrinfo failed with socket.gaierror[11001] (python) (mqtt)

  28. 28

    socket.gaierror: Errno 11004 getaddrinfo failed

  29. 29

    connect to socket failed - errno 88 (cpp)

HotTag

Archive