Python gaierror: [Errno 11004] getaddrinfo 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37469680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:25:57  来源:igfitidea点击:

gaierror: [Errno 11004] getaddrinfo failed

pythonsockets

提问by Dawn

I hope to access the text file from the following url:

我希望从以下网址访问文本文件:

http://www.pythonlearn.com/code/intro-short.txt

http://www.pythonlearn.com/code/intro-short.txt

My code is

我的代码是

import socket

socket.getaddrinfo('127.0.0.1', 8080)
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('http://www.pythonlearn.com', 80))
mysock.send('GET http://www.pythonlearn.com/code/intro-short.txt HTTP/1.0\n\n')

I keep getting an error: gaierror: [Errno 11004] getaddrinfo failed

我不断收到错误消息: gaierror: [Errno 11004] getaddrinfo failed

Can you help me with this?

你能帮我解决这个问题吗?

Thanks.

谢谢。

回答by Gregory D. Weber

In mysock.connect(('http://www.pythonlearn.com', 80)), the first element in the tuple should be just the host name (or address), without 'http://'.

在 中mysock.connect(('http://www.pythonlearn.com', 80)),元组中的第一个元素应该只是主机名(或地址),没有“http://”。

So mysock.connect(('www.pythonlearn.com', 80))should work.

所以mysock.connect(('www.pythonlearn.com', 80))应该工作。

Incidentally, socket.getaddrinfo('127.0.0.1', 8080)would get the address information for your local host, not the server you want to contact; so this statement seems unnecessary.

顺便说一句,socket.getaddrinfo('127.0.0.1', 8080)将获得您本地主机的地址信息,而不是您要联系的服务器;所以这个说法似乎没有必要。