9

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?

Chris
  • 2,619
  • 6
  • 27
  • 34

4 Answers4

19

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.

wpercy
  • 9,636
  • 4
  • 33
  • 45
oarevalo
  • 3,298
  • 1
  • 22
  • 19
8

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

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

Don Spaulding
  • 764
  • 5
  • 13
  • ping returns a "Packet filtered" message and fails. I'd assume that this is because it is secured and will only accept port 443. at this point I'm getting a "Permission Denied" message. I'm not sure if that's a step forward or back. – Chris Jan 12 '11 at 20:26
  • Chris, I'm able to ping secure.authorize.net successfully. What does `nslookup secure.authorize.net` tell you? – Don Spaulding Jan 12 '11 at 23:02
  • Server: 192.168.3.1 Address: 192.168.3.1#53 Non-authoritative answer: Name: secure.authorize.net Address: 64.94.118.33 Name: secure.authorize.net Address: 64.94.118.32 – Chris Jan 13 '11 at 14:20
1

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.

Chris
  • 2,619
  • 6
  • 27
  • 34
0

pass the port separately from the host:

conn = httplib.HTTPSConnection("secure.authorize.net", 443, ....)  
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • that returned "error: [Errno13] Permission denied" What exactly should my source address be? can it just me "www.mydomain.com" or should it be "http://www.mydomain.com/mypy.py"? does it even matter? – Chris Jan 12 '11 at 19:50
  • the Permission Denied" was actually caused by me changing my source_address. separating the url and port didn't seem to make any difference. – Chris Jan 12 '11 at 19:53
  • what do you mean? Now you don't get the `gaierror: [Errno -2] Name or service not known` anymore but another error? – nosklo Jan 13 '11 at 01:48
  • now i'm getting a Permission denied error. I think apache doesn't have permission to use that port or something. – Chris Jan 13 '11 at 14:18
  • 1
    @Chris: Can you update the question with updated code and the full new error traceback? – nosklo Jan 13 '11 at 15:21
  • 1
    we actually worked it out. the code was fine. 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. – Chris Jan 13 '11 at 16:05