forked from multiangle/Distributed_Microblog_Spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_data.py
67 lines (59 loc) · 2.17 KB
/
server_data.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
__author__ = 'multiangle'
#======================================================================
#----------------import package--------------------------
# import python package
import tornado.web
import tornado.ioloop
import tornado.options
from tornado.options import define,options
from pymongo import MongoClient
# import from this folder
#======================================================================
class DataServer(tornado.web.Application):
def __init__(self):
handlers=[
(r'/history_data',HistoryDataReturn),
(r'/auth',DataAuth)
]
setting=dict(
debug=True
)
tornado.web.Application.__init__(self,handlers,**setting)
class HistoryDataReturn(tornado.web.RequestHandler):
def post(self):
try:
data=eval(self.get_argument('data'))
current_id=int(self.get_argument('current_id'))
total_num=int(self.get_argument('total_num'))
len=int(self.get_argument('len'))
container_id=self.get_argument('container_id')
self.write('success')
self.finish()
client=MongoClient('localhost',27017)
db=client['microblog_spider']
collection=db.assemble_factory
# store sub pack to assemble factory
mongo_data=dict(
data =data,
current_id =current_id,
total_num =total_num,
len =len,
container_id=container_id,
type ='history'
)
result=collection.insert(mongo_data)
# print('ServerData->HistoryDataReturn: Success to get data from web')
except Exception as e:
self.write('fail to return user history')
self.finish()
print('Error:server-HistoryReturn:'
'Unable to get value from http package,Reason:')
print(e)
return
class DataAuth(tornado.web.RequestHandler):
def get(self):
self.write('connection success')
self.finish()
if __name__=='__main__':
DataServer().listen(8001)
tornado.ioloop.IOLoop.instance().start()