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

标签 python xml httplib

我正在尝试使用 python 和 httplib 模块连接到 FreshBooks API。我已经设法使用 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

在我使用 Packages 的第二个脚本中,我得到了相同的响应,我修复了在 post() 函数中添加 header 的问题:

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 是一种安全的编码选项,还是有更安全的方法?谢谢。

最佳答案

您实际上需要在请求中发送 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()

希望对您有帮助!

关于python - 在 Python 中使用 httplib 无法获得成功的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18763427/

相关文章:

python - 如何使用 aiohttp 检查 SSL 证书到期日期?

android - ReToucherView的onTouchListener已禁用其滚动

python - 无法使用 Python 中的 Mechanize 连接到安全网站

python - SSL 握手错误 : [Errno 1]

python - 如何使用 httplib 发布 unicode 字符?

jquery - 通过 OpenERP-7 中的特定按钮以只读模式打开表单

python - 如何找到列表中项目的索引,但不是完全匹配

python - 将 mat 文件转换为 pandas 数据帧

html - 如何使用 xsl :if 选择正确的节点值

java附加到文件