Python httplib POST请求和正确的格式

2024-10-01 15:30:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在研究一种与安装了restfulwebservices的数据库网站接口的自动化方法。我对如何使用python正确发送以下站点中列出的请求的正确格式有疑问。 https://neesws.neeshub.org:9443/nees.html

具体例子如下:

POST https://neesws.neeshub.org:9443/REST/Project/731/Experiment/1706/Organization

<Organization id="167"/>

最大的问题是我不知道把上面的XML格式的部分放在哪里。我想将上面的内容作为pythonhttps请求发送,到目前为止,我已经尝试了以下结构。在

^{pr2}$

但这似乎是完全错误的。当谈到webservices接口时,我实际上从来没有做过python,所以我的主要问题是我到底应该如何使用httplib发送POST请求,尤其是其中XML格式的部分?感谢任何帮助。在


Tags: 方法httpsorg数据库站点网站格式service
1条回答
网友
1楼 · 发布于 2024-10-01 15:30:34

在发送数据之前,您需要设置一些请求头。例如,将内容类型设置为“text/xml”。看看几个例子

Post-XML-Python-1

以以下代码为例:

import sys, httplib

HOST = www.example.com
API_URL = /your/api/url

def do_request(xml_location):
"""HTTP XML Post requeste"""
request = open(xml_location,"r").read()
webservice = httplib.HTTP(HOST)
webservice.putrequest("POST", API_URL)
webservice.putheader("Host", HOST)
webservice.putheader("User-Agent","Python post")
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(request))
webservice.endheaders()
webservice.send(request)
statuscode, statusmessage, header = webservice.getreply()
result = webservice.getfile().read()
    print statuscode, statusmessage, header
    print result

do_request("myfile.xml")

Post-XML-Python-2

你可能会有一些想法。在

相关问题 更多 >

    热门问题