python – Google Safe-Browsing Lookup API中的无效请求
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Google Safe-Browsing Lookup API中的无效请求,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1880字,纯文字阅读大概需要3分钟。
内容图文
![python – Google Safe-Browsing Lookup API中的无效请求](/upload/InfoBanner/zyjiaocheng/769/7cefa0365a68470e8319bfe196e86b8b.jpg)
我正在尝试向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_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()
解决方法:
您似乎没有遵循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
内容总结
以上是互联网集市为您收集整理的python – Google Safe-Browsing Lookup API中的无效请求全部内容,希望文章能够帮你解决python – Google Safe-Browsing Lookup API中的无效请求所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。