Django请求具有SSL IO错误的XML文件 [英] Django request XML file with SSL IO error

查看:329
本文介绍了Django请求具有SSL IO错误的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Django网站,并且是相当新的。在这个webapp中,我需要使用这个API,它将从数据库中吐出一个带有请求的数据的xml文件。
基本上API的URL是:



https:// adminuser:password@url.to.database.com/database.getdata?arg = 1& arg2 = 0



所以在我的python views.py我有:

  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()
返回xml_parser.fromstring(rawdata)

我从 http://www.webmonkey.com/2010获得的/ 02 / integrate_web_apis_into_your_django_site /



但是,我在线上收到以下错误 usock = u.open(url)

 在/ webapp / 

的IOError [Errno s ocket error] [Errno 1] _ssl.c:480:error:140943FC:SSL例程:SSL3_READ_BYTES:sslv3 alert bad record mac

我读了 urllib 文档,如果无法建立连接,则会引发IOError。
http://docs.python.org/library/urllib.html
此外,在维基百科上,坏记录MAC致命警报意味着可能是一个坏的SSL实现,或有效载荷被篡改,例如,FTPS服务器上的FTP防火墙规则。



但是我不明白的是,当我将URL粘贴到浏览器中时,它可以正常工作,并且会吐出一个XML文件。



也认为(作为一个长镜头)它可能是我的Apache安装,所以我检查了mod_ssl是否正在加载,在终端中键入 apachectl -t -D DUMP_MODULES ,它被加载为共享。



任何想法都将不胜感激。谢谢!

解决方案

我的同事让API在PHP中工作,所以我看了他的代码,他正在使用卷曲。我发现有一个名为 PycURL 的python版本。安装PycURL后,我撕掉了 urllib 代码,并使用了PycURL。

  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(兼容; MSIE 5.01; Windows NT 5.0) ')

outputXML = c.perform()
c.close()

我猜 urllib 不如PycURL那么强壮。


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!

解决方案

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 urllibis not as robust as PycURL.

这篇关于Django请求具有SSL IO错误的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆