python httplib Name or service not known

Chris picture Chris · Jan 12, 2011 · Viewed 48.1k times · Source

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:

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.

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?

Answer

oarevalo picture oarevalo · Oct 6, 2011

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.