在Python中使用httplib无法获得成功的响应

问题描述 投票:1回答:3

我正在尝试使用python和FreshBooks API模块连接到httplib。我已经成功地使用Requests软件包完成了这项工作,但是由于我是一个入门者,并且想学习,所以我也想使用标准的Python库使其工作。

这就是使用httplib的代码:

import base64, httplib

# test script created to connect with my test Freshbooks account

headers = {}
body = '/api/2.1/xml-in'
headers["Authorization"] = "Basic {0}".format(
    base64.b64encode("{0}:{1}".format('I have put here my Auth Token', 'user')))
headers["Content-type"] = "application/xml"

# the XML we ll send to Freshbooks
XML = """<?xml version="1.0" encoding="utf-8"?>
<request method="task.list">
  <page>1</page>
  <per_page>15</per_page>
</request>"""


# Enable the job
conn = httplib.HTTPSConnection('devjam-billing.freshbooks.com')
conn.request('POST', body, None, headers)
resp = conn.getresponse()
print resp.status
conn.send(XML)

print resp.read()
conn.close()

这就是Freshbooks返回的内容:

200                                                                                                                                                                                                                                                                               
<?xml version="1.0" encoding="utf-8"?>                                                                                                                                                                                                                                            
<response xmlns="http://www.freshbooks.com/api/" status="fail">                                                                                                                                                                                                                   
  <error>Your XML is not formatted correctly.</error>                                                                                                                                                                                                                             
  <code>40010</code>                                                                                                                                                                                                                                                              
</response

在我使用包的第二个脚本中,我得到了与在post()函数中添加标头固定的相同响应:

import requests

XML = """<?xml version="1.0" encoding="utf-8"?>
<request method="task.list">
    <page>1</page>
    <per_page>15</per_page>
</request>"""
headers = {'Content-Type': 'application/xml'} # set what your server accepts
 r = requests.post('https://devjam-billing.freshbooks.com/api/2.1/xml-in', auth=      ('my auth token', 'user'), data=XML, headers=headers)

 print r.status_code
 print r.headers['content-type']
 # get the response
 print r.text

我尝试通过添加以下内容来做类似的事情:

headers["Content-type"] = "application/xml"

没有成功。

有什么想法吗? b64encode还是一种安全的编码安全选项,还是有更安全的方法呢?谢谢。

python xml httplib
3个回答
1
投票

您实际上需要在请求中发送POST数据(XML字符串),因此,请替换为:

conn.request('POST', body, None, headers)
resp = conn.getresponse()
print resp.status
conn.send(XML)
print resp.read()

带有此:

conn.request('POST', body, XML, headers)
resp = conn.getresponse()
print resp.status
print resp.read()

希望对您有所帮助!


0
投票

您似乎在错误的时间发送数据。从docs看(重点是我的)。

HTTPConnection.send(数据)

将数据发送到服务器。仅应在after endheaders()方法已被调用且before getresponse()被调用的情况下直接使用此方法。

根据您的情况:

conn.endheaders()
conn.send(XML)
resp = conn.getresponse()

0
投票

我一直试图将使用此脚本的脚本移植到Python 3。

我遇到的主要障碍是'需要一个类似字节的对象,而不是'str'。虽然我大致了解问题所在,但仍很难解决它。

有人可以帮忙吗?预先感谢!

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.