3

I use google safe browsing API. So I tested this simple code:

from safebrowsinglookup import SafebrowsinglookupClient

class TestMe:
    def testMe(self):
        self.key='my_Valid_Key_Here'        
        self.client=SafebrowsinglookupClient(self.key)
        self.response=self.client.lookup('http://www.google.com')
        print(self.response)

if __name__=="__main__":
    TM=TestMe()
    TM.testMe()

I always get this whatever the website I test:

{'website_I_tried','error'}

Note that I had to change some lines in the source code after I installed this API because it was written in Python 2 and I am using Python 3.4.1. How can I resolve this problem?


Update:

To understand why the above problem occured to me, I run this code:

from safebrowsinglookup import SafebrowsinglookupClient  

class TestMe:
    def testMe(self):
        self.key = 'my_key_here'        
        self.client=SafebrowsinglookupClient(self.key,debug=1)
        urls = ['http://www.google.com/','http://addonrock.ru/Debugger.js/']        
        self.results = self.client.lookup(*urls)
        print(self.results['http://www.google.com/'])

if __name__ == "__main__":
    TM=TestMe()
    TM.testMe()

Now, I got this message:

BODY:
2
http://www.google.com/
http://addonrock.ru/Debugger.js/


URL: https://sb-ssl.google.com/safebrowsing/api/lookup?client=python&apikey=ABQIAAAAAU6Oj8JFgQpt0AXtnVwBYxQYl9AeQCxMD6irIIDtWpux_GHGQQ&appver=0.1&pver=3.0
Unexpected server response

name 'urllib2' is not defined
error
error
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    The `KeyError` in the second case is occurring because `results` is a dict, not a list. Its keys are the websites you looked up, and you didn't look up any websites called 0 or 1. In the first case, there's absolutely no way for us to tell what error occurred based on the info you've given us. Add `debug=1` to the SafebrosinglookupClient, and that should print out the actual errors encountered. Please post **complete** error messages, including tracebacks when possible, for all problems on SO. Otherwise there's no way for us to help you. – Mark Whitfield Jul 31 '14 at 13:11
  • @user3479224 Yes, thank you, that is one first error, i will try to fix it now, but I think i will still have the problem in an other way –  Jul 31 '14 at 13:13
  • @user3479224 I update my second short code: I only get `error` as an output –  Jul 31 '14 at 13:17
  • 2
    That's because you didn't add `debug=1` to the SafebrosinglookupClient contructor like I said. – Mark Whitfield Jul 31 '14 at 13:25
  • 1
    @user3479224 Sorry, I did what you said, you can see the full error on my question now. Thank you –  Jul 31 '14 at 13:29

1 Answers1

3

The library doesn't support Python3.x.

In this case, you can either make it support Python3 (there is also an opened pull request for Python3 Compatibility), or make the request to "Google Safebrowsing API" manually.

Here's an example using requests:

import requests

key = 'your key here'
URL = "https://sb-ssl.google.com/safebrowsing/api/lookup?client=api&apikey={key}&appver=1.0&pver=3.0&url={url}"


def is_safe(key, url):
    response = requests.get(URL.format(key=key, url=url))
    return response.text != 'malware'


print(is_safe(key, 'http://addonrock.ru/Debugger.js/'))  # prints False
print(is_safe(key, 'http://google.com'))  # prints True

Just the same, but without third-party packages (using urllib.request):

from urllib.request import urlopen


key = 'your key here'
URL = "https://sb-ssl.google.com/safebrowsing/api/lookup?client=python&apikey={key}&appver=1.0&pver=3.0&url={url}"


def is_safe(key, url):
    response = urlopen(URL.format(key=key, url=url)).read().decode("utf8")
    return response != 'malware'


print(is_safe(key, 'http://addonrock.ru/Debugger.js/'))  # prints False
print(is_safe(key, 'http://google.com'))  # prints True
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    Thank you. I get a strange error however: ImportError: no module named requests`` even if i am using Python-3.4.1 (!) –  Jul 31 '14 at 13:41
  • 2
    @Begueradj `requests` is a third-party, you need to install it first. Ok, let me rewrite it using only stdlib packages. – alecxe Jul 31 '14 at 13:42
  • Yes, Alexander, I bothered you with my comment while on the link of your answser it says I must install it. So i am installing it now and I will test again. Regards –  Jul 31 '14 at 13:43
  • 1
    @Begueradj glad to help, I've added only-standard library version. – alecxe Jul 31 '14 at 13:47