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 Dev

socket.gaierror: Errno 11004 getaddrinfo failed

From Dev

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

From Dev

gaierror: [Errno 11004] getaddrinfo failed

From Dev

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

From Dev

connect to socket failed - errno 88 (cpp)

From Dev

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

From Dev

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

From Dev

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

From Dev

socket connect() returns errno EINVAL

From Dev

OpenSSL: socket: Connection refused connect:errno=111

From Dev

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

From Dev

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

From Dev

Python - Telnetlib socket.gaierror:

From Java

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

From Dev

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

From Dev

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

From Dev

(python) [Errno 11001] getaddrinfo failed

From Dev

(python) [Errno 11001] getaddrinfo failed

From Dev

Netty connect to unix domain socket failed

From Dev

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

From Dev

Pycharm python console socket.gaierror

From Dev

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

From Dev

urlopen error [Errno 11001] getaddrinfo failed

From Dev

urlopen error [Errno 11001] getaddrinfo failed?

From Dev

Server Socket errno 57 - Socket is not connected

From Dev

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

From Dev

Tornado Python blocking on _socket.getaddrinfo

From Dev

Why am I getting socket.gaierror with httplib?

From Dev

socket.gaierror when trying to run emr using python mrjob

Related Related

  1. 1

    socket.gaierror: Errno 11004 getaddrinfo failed

  2. 2

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

  3. 3

    gaierror: [Errno 11004] getaddrinfo failed

  4. 4

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

  5. 5

    connect to socket failed - errno 88 (cpp)

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

    socket connect() returns errno EINVAL

  10. 10

    OpenSSL: socket: Connection refused connect:errno=111

  11. 11

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

  12. 12

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

  13. 13

    Python - Telnetlib socket.gaierror:

  14. 14

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

  15. 15

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

  16. 16

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

  17. 17

    (python) [Errno 11001] getaddrinfo failed

  18. 18

    (python) [Errno 11001] getaddrinfo failed

  19. 19

    Netty connect to unix domain socket failed

  20. 20

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

  21. 21

    Pycharm python console socket.gaierror

  22. 22

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

  23. 23

    urlopen error [Errno 11001] getaddrinfo failed

  24. 24

    urlopen error [Errno 11001] getaddrinfo failed?

  25. 25

    Server Socket errno 57 - Socket is not connected

  26. 26

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

  27. 27

    Tornado Python blocking on _socket.getaddrinfo

  28. 28

    Why am I getting socket.gaierror with httplib?

  29. 29

    socket.gaierror when trying to run emr using python mrjob

HotTag

Archive