Browse Source

urllib2 -> requests iekš logina

Ivars 5 years ago
parent
commit
a6f802d653
1 changed files with 42 additions and 23 deletions
  1. 42
    23
      ltcproxy.py

+ 42
- 23
ltcproxy.py View File

12
 __version__ = "0.1b"
12
 __version__ = "0.1b"
13
 
13
 
14
 import os, sys, time
14
 import os, sys, time
15
-import urllib,urlparse, urllib2, requests
15
+import urllib
16
 from urllib import unquote, quote
16
 from urllib import unquote, quote
17
-import re, json
17
+import urlparse
18
+import requests
19
+import re
20
+import json
18
 import ConfigParser, getopt
21
 import ConfigParser, getopt
19
 import arrow
22
 import arrow
20
 from diskcache import Cache
23
 from diskcache import Cache
21
 import daemonize
24
 import daemonize
22
 import bottle
25
 import bottle
23
 from bottle import Bottle, hook, response, route, request, run
26
 from bottle import Bottle, hook, response, route, request, run
27
+import urllib3
28
+urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
29
+
24
 
30
 
25
 cunicode = lambda s: s.decode("utf8") if isinstance(s, str) else s
31
 cunicode = lambda s: s.decode("utf8") if isinstance(s, str) else s
26
 cstr = lambda s: s.encode("utf8") if isinstance(s, unicode) else s
32
 cstr = lambda s: s.encode("utf8") if isinstance(s, unicode) else s
474
 Host: manstv.lattelecom.tv
480
 Host: manstv.lattelecom.tv
475
 """ )
481
 """ )
476
     try:
482
     try:
477
-        r = urllib2.Request(url, data=params, headers=headers)
478
-        u = urllib2.urlopen(r)
479
-        content = u.read()
480
-        u.close()
483
+        r = requests.post(url, data=params, headers=headers)
481
     except Exception as ex:
484
     except Exception as ex:
482
         if DEBUG:
485
         if DEBUG:
483
-            print "Login error: %s - %s" % (ex.code, ex.msg)
484
-            print ex.hdrs
486
+            print "Login exception - %s " % (str(ex))
487
+            if "hdrs" in dir(ex):
488
+                print ex.hdrs
485
         return None
489
         return None
486
-    if u and "token" in content:
487
-        token = re.search('"token":"(.+?)"', content).group(1)
488
-        return token
489
-    else:
490
+    if not r.ok:
491
+        if DEBUG:
492
+            print "Login error - %s: %s" % (r.status_code, r.reason)
493
+        return None
494
+
495
+    content = r.content
496
+    if not "token" in content:
490
         if DEBUG:
497
         if DEBUG:
491
             print "Error searching token in response"
498
             print "Error searching token in response"
492
             print content
499
             print content
493
-        return False
500
+        return None
501
+    token = re.search('"token":"(.+?)"', content).group(1)
502
+    return token
503
+
494
 
504
 
495
 def refresh_token(token):
505
 def refresh_token(token):
496
     """Refresh"""
506
     """Refresh"""
501
 User-Agent: Shortcut.lv v2.9.1 / Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G900FD Build/KOT49H)
511
 User-Agent: Shortcut.lv v2.9.1 / Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G900FD Build/KOT49H)
502
 Content-Type: application/x-www-form-urlencoded; charset=UTF-8
512
 Content-Type: application/x-www-form-urlencoded; charset=UTF-8
503
 Host: manstv.lattelecom.tv
513
 Host: manstv.lattelecom.tv
504
-""" )
514
+""")
505
     try:
515
     try:
506
-        r = urllib2.Request(url, data=params, headers=headers)
507
-        u = urllib2.urlopen(r)
508
-        content = u.read()
509
-        u.close()
516
+        r = requests.post(url, data=params, headers=headers)
510
     except Exception as ex:
517
     except Exception as ex:
518
+        if DEBUG:
519
+            print "Login exception - %s " % (str(ex))
520
+            if "hdrs" in dir(ex):
521
+                print ex.hdrs
511
         return None
522
         return None
512
-    if r and "token" in content:
513
-        token2 = re.search('"token":"(.+?)"', content).group(1)
514
-        return token2
515
-    else:
516
-        return False
523
+    if not r.ok:
524
+        if DEBUG:
525
+            print "Login error - %s: %s" % (r.status_code, r.reason)
526
+        return None
527
+
528
+    content = r.content
529
+    if not "token" in content:
530
+        if DEBUG:
531
+            print "Error searching token in response"
532
+            print content
533
+        return None
534
+    token2 = re.search('"token":"(.+?)"', content).group(1)
535
+    return token2
517
 
536
 
518
 def print_headers(headers):
537
 def print_headers(headers):
519
     for h in headers:
538
     for h in headers: