我正在尝试将查询发布到RSCB PDB Web服务,如here所述。
我设置了网址,并以XML格式查询:
import urllib.request as urllib
import requests
url = "http://www.rcsb.org/pdb/rest/search"
queryText = """
<?xml version="1.0" encoding="UTF-8"?>
<orgPdbQuery>
<version>B0907</version>
<queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
<description>Experimental Method Search: Experimental Method=SOLID-STATE NMR</description>
<mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
</orgPdbQuery>
"""
然后我定义了两种可能的方法来发布这些数据:
def query_old_fashioned(url, query_xml):
req = urllib.Request(url, data=query_xml.encode())
f = urllib.urlopen(req)
result = f.read()
return result.decode()
def query_with_requests(url, query_xml):
response = requests.post(url, data=query_xml.encode())
return response.text
# result = query_old_fashioned(url, queryText)
# result = query_with_requests(url, queryText)
使用第一个函数,使用老式的urllib.request,我得到了正确的结果 - 一个包含4个字符串的列表。
使用第二个函数,据我所知,完全同样的事情,我得到一个返回的JSP错误消息HTML。这是在浏览器中显示的错误消息:
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
java.util.StringTokenizer.<init>(StringTokenizer.java:199)
java.util.StringTokenizer.<init>(StringTokenizer.java:221)
org.rcsb.servlet.RestfulServiceServlet.doPost(RestfulServiceServlet.java:1371)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:86)
org.pdb.util.web.OutOfServiceFilter.doFilter(OutOfServiceFilter.java:91)
org.pdb.util.web.DOSFilter.doFilter(DOSFilter.java:158)
org.pdb.util.web.AntiRobotFilter.doFilter(AntiRobotFilter.java:29)
org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.61 logs.
我知道一个小JSP,但是我还没有能够从这个错误消息中找出POST失败的原因,也不清楚为什么请求失败但标准库的urllib成功了。我甚至试图在github上查看请求库的源代码,试图找到请求创建请求的确切方式,但是我没有成功完成这项工作。
这是使用Python 3.我首先使用Ubuntu遇到了这个问题,并且已经在Windows 10上重现了它。
任何帮助都会非常感激。
答案 0 :(得分:3)
我设法解决了这个问题。
我检查了正在发送的HTTP请求,并看到请求发送了这个:
POST /pdb/rest/search HTTP/1.1
Host: www.rcsb.org
User-Agent: python-requests/2.8.1
Connection: keep-alive
Accept: */*
Content-Length: 316
Accept-Encoding: gzip, deflate
<?xml version="1.0" encoding="UTF-8"?>
<orgPdbQuery>
<version>B0907</version>
<queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
<description>Experimental Method Search: Experimental Method=SOLID-STATE NMR</de
scription>
<mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
</orgPdbQuery>
...而且urllib发送了这个......
POST /pdb/rest/search HTTP/1.1
Accept-Encoding: identity
Content-Type: application/x-www-form-urlencoded
Content-Length: 316
User-Agent: Python-urllib/3.4
Connection: close
Host: www.rcsb.org
<?xml version="1.0" encoding="UTF-8"?>
<orgPdbQuery>
<version>B0907</version>
<queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
<description>Experimental Method Search: Experimental Method=SOLID-STATE NMR</de
scription>
<mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
</orgPdbQuery>
有一些标题不同,通过玩它们,我发现它是请求请求中需要的Content-Type标题。
现在可以使用以下内容:
response = requests.post(
url,
data=query_xml.encode(),
headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
感谢Philipp运行我的原始代码并验证这在技术上是可行的。我怀疑他的请求版本不同于我。
答案 1 :(得分:0)
在我的Ubuntu机器上它工作正常。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import urllib.request as urllib
def query_old_fashioned(url, query_xml):
req = urllib.Request(url, data=query_xml.encode())
f = urllib.urlopen(req)
result = f.read()
return result.decode()
def query_with_requests(url, query_xml):
response = requests.post(url, data=query_xml.encode())
return response.text
def test():
url = "http://www.rcsb.org/pdb/rest/search"
query = """
<?xml version="1.0" encoding="UTF-8"?>
<orgPdbQuery>
<version>B0907</version>
<queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
<description>Experimental Method Search: Experimental Method=SOLID-STATE NMR</description>
<mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
</orgPdbQuery>"""
print(query_old_fashioned(url, query))
print(query_with_requests(url, query))
if __name__ == '__main__':
test()
print("done")
两者都打印出来。您使用的是什么版本的python?我在Ubuntu 14.03上使用Python 3.4.3