【问题标题】:Invalid Request in Google Safe-Browsing Lookup APIGoogle 安全浏览查找 API 中的请求无效
【发布时间】:2011-06-14 11:28:22
【问题描述】:

我正在尝试向 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()

【问题讨论】:

    标签: python google-app-engine safe-browsing


    【解决方案1】:

    您似乎没有遵循 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 请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多