Django 请求 XML 文件时出现 SSL IO 错误

发布于 2024-09-07 17:52:40 字数 1538 浏览 14 评论 0原文

我正在制作一个 Django 网站,而且还很新。在此 Web 应用程序中,我需要使用此 API,它将生成一个 xml 文件,其中包含数据库中请求的数据。 基本上,API URL 是:

https:// adminuser:[email protected]/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()
  return xml_parser.fromstring(rawdata)

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

但是,我在 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

我在 urllib 文档中读到,如果无法建立连接。 http://docs.python.org/library/urllib.html 此外,在维基百科上,“错误记录 MAC”致命警报意味着“可能是错误的 SSL 实施,或者有效负载已被篡改。例如,FTPS 服务器上的 FTP 防火墙规则。”

但我不明白的是,当我将 URL 粘贴到浏览器中时,它工作正常并输出 XML 文件。

我还认为(从长远来看)这可能是我的 Apache 安装,所以我通过在终端中输入 apachectl -t -D DUMP_MODULES 来检查 mod_ssl 是否正在加载,并且它以共享方式加载。

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

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:[email protected]/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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身仙ぐ女味 2024-09-14 17:52:40

我的同事让 API 在 PHP 中工作,所以我查看了他的代码,发现他正在使用 cURL。我发现有一个名为 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 (compatible; MSIE 5.01; Windows NT 5.0)')

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

我猜 urllib 不如 PycURL 健壮。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文