问题
I'm making a Django website and am fairly new. In this webapp I need to use this API which will spit out an xml file with the requested data from the database. Basically the API URL is:
https://adminuser:password@url.to.database.com/database.getdata?arg=1&arg2=0
So in my python views.py I have:
def fetch_xml(url):
import urllib
import xml.etree.cElementTree as xml_parser
u = urllib.URLopener(None)
usock = u.open(url)
rawdata = usock.read()
usock.close()
return xml_parser.fromstring(rawdata)
Which I got from http://www.webmonkey.com/2010/02/integrate_web_apis_into_your_django_site/
However, I received the following error right at the line usock = u.open(url)
IOError at /webapp/
[Errno socket error] [Errno 1] _ssl.c:480: error:140943FC:SSL routines:SSL3_READ_BYTES:sslv3 alert bad record mac
I read on the urllib
documentation that an IOError is thrown if the connection cannot be made.
http://docs.python.org/library/urllib.html
Also, on Wikipedia a "Bad record MAC" fatal alert means "Possibly a bad SSL implementation, or payload has been tampered with. E.g., FTP firewall rule on FTPS server."
But what I don't understand is that when I paste the URL into my browser it works fine and spits out an XML file.
I also thought (as a long shot) it might be my Apache installation so I checked that mod_ssl was being loaded by typing apachectl -t -D DUMP_MODULES
in terminal and it is loaded as shared.
Any ideas would be greatly appreciated. Thanks!
回答1:
My coworker got the API to work in PHP so I took a look at his code and he was using cURL
. I found out there is a python version called PycURL
. After installing PycURL, I ripped out the urllib
code and used PycURL instead.
import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, authenticate_url)
c.setopt(pycurl.SSLVERSION, 3)
c.setopt(pycurl.SSL_VERIFYPEER, False)
c.setopt(pycurl.SSL_VERIFYHOST, 2)
c.setopt(pycurl.USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)')
outputXML = c.perform()
c.close()
I guess urllib
is not as robust as PycURL.
来源:https://stackoverflow.com/questions/3134666/django-request-xml-file-with-ssl-io-error