-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpThread.py
133 lines (122 loc) · 3.69 KB
/
HttpThread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import urllib
import urllib2
import thread
import threading
import MultipartPostHandler
import cookielib
class HttpThread(threading.Thread):
# def on_thread_finished(self, thread, data):
# pass
#def __init__(self):
#self._is_running = False
def setup(self, url, values, callback, action):
self.url = url
self.values = values
self.callback = callback
self.action = action
def login_routine(self):
try:
if self.values != None:
data = urllib.urlencode(self.values)
req = urllib2.Request(self.url, data)
response = urllib2.urlopen(req)
result = response.read()
if self.callback != None:
self.callback(result)
except:
if self.callback != None:
self.callback("ERROR")
def download_routine(self):
try:
req = urllib2.Request(self.url, None)
response = urllib2.urlopen(req)
result = response.read()
if self.callback != None:
self.callback(self.values['sid'], result)
except:
if self.callback != None:
self.callback(None, "ERROR")
def upload_routine(self):
#try:
cookies = cookielib.CookieJar()
#print "build_opener"
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
MultipartPostHandler.MultipartPostHandler)
#print "opener.open"
#params = { "username" : "wuhsi",
# "file" : open("filename", "rb") }
response = opener.open(self.url, self.values) #.read().strip()
#print "response.read"
result = response.read()
#print "result = " + result
if self.callback != None:
self.callback(result)
#except:
# print "except"
# if self.callback != None:
# self.callback("ERROR")
def query_routine(self):
try:
if self.values != None:
data = urllib.urlencode(self.values)
url = self.url + '?' + data
req = urllib2.Request(url, None)
response = urllib2.urlopen(req)
if self.callback != None:
self.callback(response)
except:
if self.callback != None:
self.callback("ERROR")
def echo_routine(self):
try:
url = self.url
req = urllib2.Request(url, None)
response = urllib2.urlopen(req)
result = response.read()
if self.callback != None:
self.callback(result)
except:
if self.callback != None:
self.callback("ERROR")
def interact_routine(self):
try:
if self.values != None:
data = urllib.urlencode(self.values)
req = urllib2.Request(self.url, data)
response = urllib2.urlopen(req)
result = response.read()
if self.callback != None:
self.callback(result)
except:
if self.callback != None:
self.callback("ERROR")
def querylike_routine(self):
try:
if self.values != None:
data = urllib.urlencode(self.values)
url = self.url + '?' + data
req = urllib2.Request(url, None)
response = urllib2.urlopen(req)
result = response.read()
if result == '1' and self.callback != None:
self.callback(self.values['music_id'], True)
elif result == '0' and self.callback != None:
self.callback(self.values['music_id'], False)
except:
if self.callback != None:
self.callback("ERROR")
def run(self):
if self.action == "LOGIN":
self.login_routine()
elif self.action == "ECHO":
self.echo_routine()
elif self.action == "UPLOADTHUMB":
self.upload_routine()
elif self.action == "LISTRECENT":
self.query_routine()
elif self.action == "DOWNLOAD":
self.download_routine()
elif self.action == "INTERACT":
self.interact_routine()
elif self.action == "ISLIKE":
self.querylike_routine()