python httplib Name or service not known

51,317

Solution 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.

Solution 2

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.

Solution 3

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?

Share:
51,317
Chris
Author by

Chris

Updated on July 09, 2022

Comments

  • Chris
    Chris almost 2 years

    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
    Chris over 13 years
    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?
  • Chris
    Chris over 13 years
    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
    Chris over 13 years
    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.
  • Don Spaulding
    Don Spaulding over 13 years
    Chris, I'm able to ping secure.authorize.net successfully. What does nslookup secure.authorize.net tell you?
  • nosklo
    nosklo over 13 years
    what do you mean? Now you don't get the gaierror: [Errno -2] Name or service not known anymore but another error?
  • Chris
    Chris over 13 years
    now i'm getting a Permission denied error. I think apache doesn't have permission to use that port or something.
  • Chris
    Chris over 13 years
    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
  • nosklo
    nosklo over 13 years
    @Chris: Can you update the question with updated code and the full new error traceback?
  • Chris
    Chris over 13 years
    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.