Python Google安全浏览查找API中的请求无效
我正在尝试向Google安全浏览查找API发送请求。它要求用户为他想要查找的url提供一个链接。但是,我发送请求的方式存在一些问题,因为它一直以错误代码400进行响应。请帮忙Python Google安全浏览查找API中的请求无效,python,google-app-engine,safe-browsing,Python,Google App Engine,Safe Browsing,我正在尝试向Google安全浏览查找API发送请求。它要求用户为他想要查找的url提供一个链接。但是,我发送请求的方式存在一些问题,因为它一直以错误代码400进行响应。请帮忙 import urllib import urllib2 from google.appengine.ext import webapp from google.appengine.ext.webapp import util from google.appengine.api import urlfetch req_u
import urllib
import urllib2
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import urlfetch
req_url = "https://sb-ssl.google.com/safebrowsing/api/lookup"
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write("""<html>
<body>
<form action='' method='POST'>
<input type='text' name='url'>
<input type='submit' value='submit!!'>
</form>
</body>
</html>""")
def post(self):
post_data = {'client':'api',
'apikey':'My-API-Key',
'appver':'1.5.2',
'pver':'3.0',
'url':"%s"% self.request.get('url') }
data = urllib.urlencode(post_data)
try:
req = urlfetch.fetch(url = req_url,
payload = data,
method = urlfetch.POST,
headers = {'Content-Type': 'application/x-www-form-urlencoded'})
if req.status_code == '200':
self.response.out.write("success")
self.response.out.write(req.content)
else:
self.response.out.write("Error code %s!!!"% req.status_code)
except urllib2.URLError, e:
self.response.out.write("Exception Raised")
handleError(e)
def main():
application = webapp.WSGIApplication([
('/', MainHandler)
],debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
导入urllib
导入urllib2
从google.appengine.ext导入webapp
从google.appengine.ext.webapp导入util
从google.appengine.api导入urlfetch
请求url=”https://sb-ssl.google.com/safebrowsing/api/lookup"
类MainHandler(webapp.RequestHandler):
def get(自我):
self.response.out.write(“”)
""")
def post(自我):
post_数据={'client':'api',
“apikey”:“My-API-Key”,
“appver”:“1.5.2”,
“pver”:“3.0”,
“url”:“%s”%self.request.get('url')}
data=urllib.urlencode(post_数据)
尝试:
req=urlfetch.fetch(url=req\U url,
有效载荷=数据,
方法=urlfetch.POST,
headers={'Content-Type':'application/x-www-form-urlencoded'})
如果req.status_code==“200”:
自我、回应、输出、书写(“成功”)
self.response.out.write(请求内容)
其他:
self.response.out.write(“错误代码%s!!!%req.status\u code”)
除urllib2.URLError外,e:
self.response.out.write(“引发异常”)
扶手箭头(e)
def main():
application=webapp.WSGIApplication([
(“/”,MainHandler)
],debug=True)
util.run_wsgi_应用程序(应用程序)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
您似乎没有遵循GET或POST方法的协议,而是通过POST传递应该是GET参数的内容,在两者之间做了一些事情
尝试以下方法:
import urllib
from google.appengine.api import urlfetch
def safe_browsing(url):
"""Returns True if url is safe or False is it is suspect"""
params = urllib.urlencode({
'client':'api',
'apikey':'yourkey',
'appver':'1.5.2',
'pver':'3.0',
'url': url })
url = "https://sb-ssl.google.com/safebrowsing/api/lookup?%s" % params
res = urlfetch.fetch(url, method=urlfetch.GET)
if res.status_code >= 400:
raise Exception("Status: %s" % res.status_code)
return res.status_code == 204
这将像:
>>> safe_browsing('http://www.yahoo.com/')
True
谢谢你,克里斯。现在它就像一个符咒。我想我需要深入研究Http请求。