Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Sun Feb 18 13:36:15 2018
- @author: Richard
- AAAAB3NzaC1yc2EAAAABJQAAAQEAwxk/qzNvj/XEploppD7RuJQRqoP8bOUDjTiAYvzCCgiBp9HnHrALbiyRFQcNWtQY3j0tlFZ1qhEju78Pe9wWsgw5+mlcTsX15vDcmMF6oF4IctSQpyrnQnzUs7F+QuVnPkfKT22VA4y9id5K5rHQkNb0HOqMl5PMn3pi+ALzy8RsYgyow5YhNNE7gy/nTkZhdjkJimB+pTWl6Qj8IxgWpm9v6YXOoaEU2pZMCaqA8LiGHrj8Xl6wV0T7xF2HgK48vISlWpDFc/zfJtMFtuud1esMYNoGePoCGHn932chfeWR38e08EZGcUPVwimU4Av03rqa1MdmjRhT7SH4xbKhtQ== rsa-key-20171206
- """
- import requests,cPickle
- import utils,msgs
- import threading
- from sync import Sync
- class HTTPTranceiver:
- def __init__(self,db,auth,l):#everything authenticated because of nazis
- self.log = l
- self.auth = auth
- self.back_table={}
- self.db=db
- self.log.info('creating HTTP thread')
- self.psthread= threading.Thread(target=self.dthread)
- self.s=requests.Session()
- self.ready=threading.Event()
- def start(self):
- self.log.info('starting HTTP thread')
- self.psthread.start()
- self.ready.wait()
- def triggerRdy(self):
- self.ready.set()
- def dthread(self):
- self.ps=self.db.pubsub()
- self.ps.subscribe(**{'HTTP':self.recv_HTTP})
- for x in self.ps.listen():
- if x['type']=='subscribe':
- self.triggerRdy()
- pass
- def send_s(self, **kwargs):
- self.log.info('starting sync send')
- s=Sync()
- self.send(s, **kwargs)
- s.w()
- return s.dat
- def send(self, back,**kwargs):
- self.log.info('preparing async')
- r=requests.Request(auth=self.auth,**kwargs)
- p=self.s.prepare_request(r)
- p.headers.update(utils.makeREQ())
- ps=cPickle.dumps(p)
- d= utils.getHash(p.headers,msgs.REQ)
- self.back_table[d]=back
- self.log.info('sending req over HTTP channel')
- self.db.publish('HTTP',ps)
- def recv_HTTP(self,m):
- if type(cPickle)==type(None):
- self.log.warning('Module error')
- return
- r = cPickle.loads(m['data'])
- if type(r)==type(None):
- self.log.warning('bad pickle in HTTP channel')
- return
- if type(r)!=requests.Response:
- self.log.info('Not a resp')
- return #message intended for uplink
- if type(r.request)==type(None):
- self.log.warning('request object missing from response')
- return
- d=utils.getHash(r.request.headers,msgs.REQ)
- if not self.back_table.has_key(d):
- self.log.info('callback not found')
- return
- else:
- self.log.info('calling back')
- self.back_table[d](r)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement