使用Python请求库 [英] Using Python Requests library

查看:111
本文介绍了使用Python请求库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有你爱的人,我有另一个为你。
我正在使用django,请求和Google结帐。我正在将XML发送到Google结帐权。一切都很好,除了使用请求库,我得到了一些我不想在POST中的内容。让我解释。
所以google想要一个正确的XML文件,得到这个,我正在使用一个可爱的库来从架构中创建数据结构。所以我的XML是正确的。请求发送到谷歌。

   -  178.32.28.118.55290.2265475.1333156904.984.1 
内容处置:形式数据; NAME = this.xml; filename =../ xml / this.xml
内容类型:application / xml

<?xml version =1.0?>
< checkout-shopping-cart xmlns ='http://checkout.google.com/schema/2'>
< shopping-cart>
< item>
<数字内容>
< url> /site_media/digitalGoods/Resume.html.pdf< / url>
< description> None Yet< / description>
< display-disposition> OPTIMISTIC< / display-disposition>
< / digital-content>
< item-name>消防安全第1部分< / item-name>
< item-description>& lt&p& p& gt。Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas。前庭弯曲曲or am,,,。。。。。,。。。。。。。。。。。 Donec eu libero sit amet quam egestas semper。 Aenean屡获殊荣的Mauris placerat eleifend利奥。
< / item-description>
< unit-price currency =USD> 1.500000e + 01< / unit-price>
< quantity> 1< / quantity>
< merchant-item-id> 1< / merchant-item-id>
< / item>
< / shopping-cart>
< checkout-flow-support>
< merchant-checkout-flow-support />
< / checkout-flow-support>
< / checkout-shopping-cart>
--178.32.28.118.55290.2265475.1333156904.984.1--

我认为的问题请求是将这些数字和那些标题放在xml上方,就像它们是一个文档一样。也是直接在xml之后编写这些数字。
我认为这是一个问题,因为我从我的谷歌集成控制台得到的错误是。

 解析XML时出错;来自解析器的消息是:在prolog中不允许内容。 

所以我的问题是:有没有办法关闭它,我需要解决请求代码我的自我,或什么。
这是我如何使用重新发送邮件

 #....其他代码和vars高于
sendfile = {'this.xml':open('../ xml / this.xml','r')}#文件
headers = {'Authorization':(Basic%s%auth) ,#google specific headers
'Content-Type':'application / xml; charset = UTF-8',
'Accept':'application / xml; charset = UTF-8'}
#send POST
r = requests.post(diagnose_turl,files = sendfile,headers = headers,verify = False)
pre>

解决方案

问题似乎在于您尝试不仅解析XML,还要解析内容类型标题,并且解析器正在抱怨,因为它期待一个XML根元素,而不是Content-Disposition字符串。



这是奇怪的,因为如果你做一些类似的事情:

  response = requests.post(some_url,...)

response.text不应该包含头。如果您使用原始响应,请切换到 response.text



如果您正在获取标题在将第一个空白行( \\\\\
)之前删除所有内容,然后将其提供给解析器:

  import re 
xml ='\\\
'.join(re.split(r'\r?\\\
\r ?\\\
',raw_response)[1:])


Hey all you loved peoples, I have another one for you. I'm using django, requests, and google checkout. I am at the point of sending xml to Google checkout right. All is well EXCEPT. Using the requests library I am getting some content that I don't want in the POST. Let me explain. So google want s a correct XML file, got that, I'm using a sweet library to make Data structure from the schema. So my XML is correct. Requests though sends this to google.

--178.32.28.118.55290.2265475.1333156904.984.1
Content-Disposition: form-data; name="this.xml"; filename="../xml/this.xml"
Content-Type: application/xml

<?xml version="1.0" ?>
<checkout-shopping-cart xmlns='http://checkout.google.com/schema/2'>
<shopping-cart>
    <item>
        <digital-content>
            <url>/site_media/digitalGoods/Resume.html.pdf</url>
            <description>None Yet</description>
            <display-disposition>OPTIMISTIC</display-disposition>
        </digital-content>
        <item-name>Fire Safety Part 1</item-name>
        <item-description>&lt;p&gt;Pellentesque habitant morbi tristique senectus et   netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
</item-description>
        <unit-price currency="USD">1.500000e+01</unit-price>
        <quantity>1</quantity>
        <merchant-item-id>1</merchant-item-id>
    </item>
</shopping-cart>
<checkout-flow-support>    
<merchant-checkout-flow-support/>
</checkout-flow-support>
</checkout-shopping-cart>
--178.32.28.118.55290.2265475.1333156904.984.1--

The problem I think is that requests is putting those numbers and those headers above the xml, like they are one document. Also it is writing those numbers directly after the xml. I think this is a problem because the error I get from my google integration console is.

 Error parsing XML; message from parser is: Content is not allowed in prolog.

SO my question is: Is there a way to turn this off, do i need to mangle the requests code my self, or what. Here is how I am POSTing with requets

#....other code and vars above
sendfile = {'this.xml':open('../xml/this.xml', 'r')}#the file
headers={'Authorization':("Basic %s" % auth),#google specific headers
        'Content-Type':'application/xml; charset=UTF-8',
        'Accept':'application/xml; charset=UTF-8'}
#send POST
r = requests.post(diagnose_turl, files=sendfile,headers=headers, verify=False)

解决方案

The problem seems to be that you are trying to parse not only the XML, but the content-type header as well, and the parser is complaining as it is expecting a XML root element, not the "Content-Disposition" string.

This is strange, because if you do something like:

response = requests.post(some_url, ...)

The response.text is not supposed to include headers. If you are using the raw response, switch to response.text instead.

If you are getting the headers anyway, get rid of everything before the first blank line (\r\n\r\n) before feeding it to the parser:

import re
xml = '\n'.join(re.split(r'\r?\n\r?\n', raw_response)[1:])

这篇关于使用Python请求库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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