python httplib 名称或服务未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4673166/
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-18 16:51:47  来源:igfitidea点击:

python httplib Name or service not known

pythonsslhttplib

提问by Chris

I'm trying to use httplib to send credit card information to authorize.net. When i try to post the request, I get the following traceback:

我正在尝试使用 httplib 将信用卡信息发送到 authorize.net。当我尝试发布请求时,我得到以下回溯:

File "./lib/cgi_app.py", line 139, in run res = method()
File "/var/www/html/index.py", line 113, in ProcessRegistration conn.request("POST", "/gateway/transact.dll", mystring, headers)
File "/usr/local/lib/python2.7/httplib.py", line 946, in request self._send_request(method, url, body, headers)
File "/usr/local/lib/python2.7/httplib.py", line 987, in _send_request self.endheaders(body)
File "/usr/local/lib/python2.7/httplib.py", line 940, in endheaders self._send_output(message_body)
File "/usr/local/lib/python2.7/httplib.py", line 803, in _send_output self.send(msg)
File "/usr/local/lib/python2.7/httplib.py", line 755, in send self.connect()
File "/usr/local/lib/python2.7/httplib.py", line 1152, in connect self.timeout, self.source_address)
File "/usr/local/lib/python2.7/socket.py", line 567, in create_connection raise error, msg
gaierror: [Errno -2] Name or service not known

I build my request like so:

我像这样构建我的请求:

mystring = urllib.urlencode(cardHash)
headers = {"Content-Type": "text/xml", "Content-Length": str(len(mystring))}
conn = httplib.HTTPSConnection("secure.authorize.net:443", source_address=("myurl.com", 443))
conn.request("POST", "/gateway/transact.dll", mystring, headers)

to add another layer to this, it was working on our development server which has httplib 2.6 and without the source_address parameter in httplib.HTTPSConnection.

要为此添加另一层,它正在我们的开发服务器上工作,该服务器具有 httplib 2.6 并且在 httplib.HTTPSConnection 中没有 source_address 参数。

Any help is greatly appreciated.

任何帮助是极大的赞赏。

===========================================================

================================================== ==========

EDIT:

编辑:

I can run it from command line. Apparently this is some sort of permissions issue. Any ideas what permissions I would need to grant to which users to make this happen? Possibly Apache can't open the port?

我可以从命令行运行它。显然这是某种权限问题。任何想法我需要授予哪些用户什么权限才能实现这一点?可能Apache无法打开端口?

采纳答案by Chris

The problem ultimately came down to the fact that selinux was stopping apache from getting that port. Disabling selinux fixed the problems. I had an issue later where i didn't have /var/www/.python-eggs/, so MySQLdb was hosing on import. But after a mkdir, it was fixed.

问题最终归结为 selinux 阻止 apache 获取该端口的事实。禁用 selinux 修复了问题。后来我遇到了一个问题,我没有 /var/www/.python-eggs/,所以 MySQLdb 正在导入。但是在 mkdir 之后,它被修复了。

回答by nosklo

pass the port separately from the host:

将端口与主机分开传递:

conn = httplib.HTTPSConnection("secure.authorize.net", 443, ....)  

回答by Don Spaulding

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

gaierror: [Errno -2] 名称或服务未知

This error often indicates a failure of your DNS resolver. Does ping secure.authorize.netreturn successful replies from the same server that receives the gaierror? Does the hostname have a typo in it?

此错误通常表示您的 DNS 解析器出现故障。是否ping secure.authorize.net从接收 gaierror 的同一服务器返回成功回复?主机名中是否有拼写错误?

回答by oarevalo

As an (obvious) heads up, this same error can also be triggered by including the protocol in the host parameter. For example this code:

作为一个(明显的)提醒,同样的错误也可以通过在主机参数中包含协议来触发。例如这段代码:

conn = httplib.HTTPConnection("http://secure.authorize.net", 80, ....)  

will also cause the "gaierror: [Errno -2] Name or service not known" error, even if all your networking setup is correct.

即使您的所有网络设置都正确,也会导致“gaierror: [Errno -2] Name or service not known”错误。