python httplib Name or service not known
Asked Answered
F

4

10

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?

Fetial answered 12/1, 2011 at 19:33 Comment(3)
What happens with a GET request, same path, no headers?Kip
same thing as on a post.Fetial
Hm, the script works fine here, even getting to The merchant login ID or password is invalid or the account is inactive. if I print the response. Focus on network issues;Kip
F
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.

Fetial answered 13/1, 2011 at 15:10 Comment(0)
B
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.

Baldwin answered 5/10, 2011 at 22:35 Comment(0)
C
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?

Creed answered 12/1, 2011 at 20:20 Comment(3)
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.Fetial
Chris, I'm able to ping secure.authorize.net successfully. What does nslookup secure.authorize.net tell you?Creed
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.32Fetial
F
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.

Fetial answered 13/1, 2011 at 15:10 Comment(0)
G
0

pass the port separately from the host:

conn = httplib.HTTPSConnection("secure.authorize.net", 443, ....)  
Glassy answered 12/1, 2011 at 19:44 Comment(6)
that returned "error: [Errno13] Permission denied" What exactly should my source address be? can it just me "www.mydomain.com" or should it be "mydomain.com/mypy.py"? does it even matter?Fetial
the Permission Denied" was actually caused by me changing my source_address. separating the url and port didn't seem to make any difference.Fetial
what do you mean? Now you don't get the gaierror: [Errno -2] Name or service not known anymore but another error?Glassy
now i'm getting a Permission denied error. I think apache doesn't have permission to use that port or something.Fetial
@Chris: Can you update the question with updated code and the full new error traceback?Glassy
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.Fetial

© 2022 - 2024 — McMap. All rights reserved.