简体   繁体   English

python httplib名称或服务未知

[英]python httplib Name or service not known

I'm trying to use httplib to send credit card information to authorize.net. 我正在尝试使用httplib将信用卡信息发送到authorize.net。 When i try to post the request, I get the following traceback: 当我尝试发布请求时,我得到以下回溯:

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可能无法打开端口?

As an (obvious) heads up, this same error can also be triggered by including the protocol in the host parameter. 作为(明显的)抬头,也可以通过在host参数中包含协议来触发同样的错误。 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]名称或服务未知”错误。

gaierror: [Errno -2] Name or service not known gaierror:[Errno -2]姓名或服务未知

This error often indicates a failure of your DNS resolver. 此错误通常表示DNS解析程序失败。 Does ping secure.authorize.net return successful replies from the same server that receives the gaierror? ping secure.authorize.net是否从收到gaierror的同一服务器返回成功的回复? Does the hostname have a typo in it? 主机名中是否有拼写错误?

The problem ultimately came down to the fact that selinux was stopping apache from getting that port. 问题最终归结为selinux阻止apache获取该端口的事实。 Disabling selinux fixed the problems. 禁用selinux修复了问题。 I had an issue later where i didn't have /var/www/.python-eggs/, so MySQLdb was hosing on import. 我之后遇到了一个问题,我没有/var/www/.python-eggs/,所以MySQLdb正在进口导入。 But after a mkdir, it was fixed. 但是在mkdir之后,它被修复了。

将端口与主机分开传递:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM