Bio*_*Rod 1 python python-requests
我越来越:
<error>You have an error in your XML syntax...
Run Code Online (Sandbox Code Playgroud)
当我运行此python脚本时,我刚刚写过(我是新手)
import requests
xml = """xxx.xml"""
headers = {'Content-Type':'text/xml'}
r = requests.post('https://example.com/serverxml.asp', data=xml)
print (r.content);
Run Code Online (Sandbox Code Playgroud)
这是xxx.xml的内容
<xml>
<API>4.0</API>
<action>login</action>
<password>xxxx</password>
<license_number>xxxxx</license_number>
<username>xxx@xyz.com</username>
<training>1</training>
</xml>
Run Code Online (Sandbox Code Playgroud)
我知道xml是有效的,因为我为perl脚本使用了相同的xml,并且内容被打印回去。
任何帮助将不胜感激,因为我是python的新手。
小智 6
您想要将XML数据从文件提供给requests.post。但是,此功能不会为您打开文件。它希望您将文件对象而不是文件名传递给它。您需要在调用request.post之前打开文件。
尝试这个:
import requests
# Set the name of the XML file.
xml_file = "xxx.xml"
headers = {'Content-Type':'text/xml'}
# Open the XML file.
with open(xml_file) as xml:
# Give the object representing the XML file to requests.post.
r = requests.post('https://example.com/serverxml.asp', data=xml)
print (r.content);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9791 次 |
| 最近记录: |