def get_access_token(self): response = False try : mverbose("Sending to: " + self.access_token_url) data = urllib2_urlopen(self.access_token_url, "", timeout=30).read() mverbose("Response: " + str(data)) response = data test_log(self.test, loc = self.access_token_url, response = data, method = "post") except IOError, e : if response : raise TranslateApiException( response.get('error_description', 'Failed to authenticate with translation service'), response.get('error', str(e)) ) else : raise TranslateApiException("Translation Service Authentication failed", str(e))
def authenticate(username, password, auth_url): try: output_dict = jabber_crypt.dumps({"username": username, "password": password}) url = auth_url + "/auth" log.write("URL: " + url + "\n") up = {"exchange": myquote(output_dict)} req = urllib2_Request(url, urllib_urlencode(up)) res = jabber_crypt.loads(urllib2_unquote(urllib2_urlopen(req).read().encode("utf-8"))) if res == "good": return 1, False elif res == "bad": return 0, "Unauthorized" else: return 0, "error" except urllib2_HTTPError, e: if e.code == 401: return 0, _("Invalid credentials. Please try again") + "." error = "(HTTP code: " + str(e.code) + ")"
#!/usr/bin/env python from urllib2 import quote as urllib2_quote, Request as urllib2_Request, urlopen as urllib2_urlopen, URLError as urllib2_URLError, HTTPError as urllib2_HTTPError from threading import Thread, Lock, current_thread, Timer, local as threading_local from time import sleep print "Request start" req = urllib2_Request("https://security.hinespot.com/file.tgz") print "Opening..." fdurl = urllib2_urlopen(req, timeout = 60) print "Grabbing socket" realsock = fdurl.fp._sock.fp._sock def get(f) : try : while True : print "Reading 1MB" f.read(1194304) print "Finished 1MB" except AttributeError, e : print "Socket closed" print "Creating thread" t = Thread(target = get, args = [fdurl]) t.daemon = True print "Starting thread" t.start() print "Sleeping" sleep(30) print "Slept"