我正在尝试使用'requests'库使以下命令行在Python中工作:
curl --header "Content-Type: text/xml;charset=UTF-8" --data @test_GetCapabilities.xml http://www.bom.gov.au/waterdata/services?service=SOS
在Anaconda Prompt中从包含test_GetCapabilities.xml文件的目录中执行时,curl立即返回所需的响应。但是当我运行下面的Python脚本时,发布请求不起作用。
import requests
url = 'http://www.bom.gov.au/waterdata/services?service=SOS'
payload = "test_GetCapabilities.xml"
headers = {'Content-Type': 'text/xml', 'charset': 'UTF-8'}
r = requests.post(url, data=open(payload), headers=headers)
print(r.content)
上面的代码在大约一分钟后超时,并给出以下堆栈:
runfile('C:/Python/SOAP_curl.py', wdir ='C:/ Python')追溯(最新 最后通话):
文件“”,第1行,在 runfile('C:/Python/SOAP_curl.py',wdir ='C:/ Python')
文件 “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”, 运行文件中的第668行 execfile(文件名,命名空间)
文件 “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”, execfile中的第108行 exec(compile(f.read(),文件名,'exec'),命名空间)
文件“ C:/Python/SOAP_curl.py”,第16行 在 r = request.post(URL,data = open(payload),headers = headers)
文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ requests \ api.py”, 第112行 返回请求('post',url,data = data,json = json,** kwargs)
文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ requests \ api.py”, 第58行,应要求 返回session.request(method = method,url = url,** kwargs)
文件 “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ requests \ sessions.py”, 512行,在请求中 resp = self.send(prep,** send_kwargs)
文件 “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ requests \ sessions.py”, 发送中的第622行 r = adapter.send(request,** kwargs)
文件 “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ requests \ adapters.py”, 发送中的第495行 引发ConnectionError(err,request = request)
ConnectionError:('连接已中止。',BadStatusLine('HTTP / 1.1 0 Init \ r \ n'))
这是test_GetCapabilities.xml的内容:
<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"
xmlns:sos="http://www.opengis.net/sos/2.0"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ows="http://www.opengis.net/ows/1.1"
xmlns:fes="http://www.opengis.net/fes/2.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:swes="http://www.opengis.net/swes/2.0"
xsi:schemaLocation="http://www.w3.org/2003/05/soap-envelope
http://www.w3.org/2003/05/soap-envelope/soap-envelope.xsd
http://www.opengis.net/sos/2.0
http://schemas.opengis.net/sos/2.0/sos.xsd">
<soap12:Header>
<wsa:To>http://www.ogc.org/SOS</wsa:To>
<wsa:Action> http://www.opengis.net/def/serviceOperation/sos/core/2.0/GetCapabilities
</wsa:Action>
<wsa:ReplyTo>
<wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:MessageID>0</wsa:MessageID>
</soap12:Header>
<soap12:Body>
<sos:GetCapabilities service="SOS"/>
</soap12:Body>
</soap12:Envelope>
答案 0 :(得分:0)
感谢@kcorlidy。
这是更正后的工作代码:
import requests
url = 'http://www.bom.gov.au/waterdata/services?service=SOS'
payload = "test_GetCapabilities.xml"
headers = {'Content-Type': 'text/xml', 'charset': 'UTF-8'}
with open(payload) as fd:
r = requests.post(url, data=fd.read().replace("\n",""), headers=headers)
print(r.content);