Преглед изворни кода

urllib2 -> requests iekš logina

Ivars пре 4 година
родитељ
комит
a6f802d653
1 измењених фајлова са 42 додато и 23 уклоњено
  1. 42
    23
      ltcproxy.py

+ 42
- 23
ltcproxy.py Прегледај датотеку

@@ -12,15 +12,21 @@ usage: %s start|stop|restart|manualstart [options]
12 12
 __version__ = "0.1b"
13 13
 
14 14
 import os, sys, time
15
-import urllib,urlparse, urllib2, requests
15
+import urllib
16 16
 from urllib import unquote, quote
17
-import re, json
17
+import urlparse
18
+import requests
19
+import re
20
+import json
18 21
 import ConfigParser, getopt
19 22
 import arrow
20 23
 from diskcache import Cache
21 24
 import daemonize
22 25
 import bottle
23 26
 from bottle import Bottle, hook, response, route, request, run
27
+import urllib3
28
+urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
29
+
24 30
 
25 31
 cunicode = lambda s: s.decode("utf8") if isinstance(s, str) else s
26 32
 cstr = lambda s: s.encode("utf8") if isinstance(s, unicode) else s
@@ -474,23 +480,27 @@ Content-Type: application/x-www-form-urlencoded; charset=UTF-8
474 480
 Host: manstv.lattelecom.tv
475 481
 """ )
476 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 484
     except Exception as ex:
482 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 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 497
         if DEBUG:
491 498
             print "Error searching token in response"
492 499
             print content
493
-        return False
500
+        return None
501
+    token = re.search('"token":"(.+?)"', content).group(1)
502
+    return token
503
+
494 504
 
495 505
 def refresh_token(token):
496 506
     """Refresh"""
@@ -501,19 +511,28 @@ def refresh_token(token):
501 511
 User-Agent: Shortcut.lv v2.9.1 / Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G900FD Build/KOT49H)
502 512
 Content-Type: application/x-www-form-urlencoded; charset=UTF-8
503 513
 Host: manstv.lattelecom.tv
504
-""" )
514
+""")
505 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 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 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 537
 def print_headers(headers):
519 538
     for h in headers: