Advertisement
Guest User

Untitled

a guest
Feb 28th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Feb 18 13:36:15 2018
  4.  
  5. @author: Richard
  6. AAAAB3NzaC1yc2EAAAABJQAAAQEAwxk/qzNvj/XEploppD7RuJQRqoP8bOUDjTiAYvzCCgiBp9HnHrALbiyRFQcNWtQY3j0tlFZ1qhEju78Pe9wWsgw5+mlcTsX15vDcmMF6oF4IctSQpyrnQnzUs7F+QuVnPkfKT22VA4y9id5K5rHQkNb0HOqMl5PMn3pi+ALzy8RsYgyow5YhNNE7gy/nTkZhdjkJimB+pTWl6Qj8IxgWpm9v6YXOoaEU2pZMCaqA8LiGHrj8Xl6wV0T7xF2HgK48vISlWpDFc/zfJtMFtuud1esMYNoGePoCGHn932chfeWR38e08EZGcUPVwimU4Av03rqa1MdmjRhT7SH4xbKhtQ== rsa-key-20171206
  7. """
  8. import requests,cPickle
  9. import utils,msgs
  10. import threading
  11. from sync import Sync
  12. class HTTPTranceiver:
  13. def __init__(self,db,auth,l):#everything authenticated because of nazis
  14. self.log = l
  15. self.auth = auth
  16. self.back_table={}
  17. self.db=db
  18. self.log.info('creating HTTP thread')
  19. self.psthread= threading.Thread(target=self.dthread)
  20. self.s=requests.Session()
  21. self.ready=threading.Event()
  22. def start(self):
  23. self.log.info('starting HTTP thread')
  24. self.psthread.start()
  25. self.ready.wait()
  26. def triggerRdy(self):
  27. self.ready.set()
  28. def dthread(self):
  29. self.ps=self.db.pubsub()
  30. self.ps.subscribe(**{'HTTP':self.recv_HTTP})
  31. for x in self.ps.listen():
  32. if x['type']=='subscribe':
  33. self.triggerRdy()
  34. pass
  35. def send_s(self, **kwargs):
  36. self.log.info('starting sync send')
  37. s=Sync()
  38. self.send(s, **kwargs)
  39. s.w()
  40. return s.dat
  41. def send(self, back,**kwargs):
  42. self.log.info('preparing async')
  43. r=requests.Request(auth=self.auth,**kwargs)
  44. p=self.s.prepare_request(r)
  45. p.headers.update(utils.makeREQ())
  46. ps=cPickle.dumps(p)
  47. d= utils.getHash(p.headers,msgs.REQ)
  48. self.back_table[d]=back
  49. self.log.info('sending req over HTTP channel')
  50. self.db.publish('HTTP',ps)
  51. def recv_HTTP(self,m):
  52. if type(cPickle)==type(None):
  53. self.log.warning('Module error')
  54. return
  55.  
  56. r = cPickle.loads(m['data'])
  57. if type(r)==type(None):
  58. self.log.warning('bad pickle in HTTP channel')
  59. return
  60.  
  61. if type(r)!=requests.Response:
  62. self.log.info('Not a resp')
  63. return #message intended for uplink
  64. if type(r.request)==type(None):
  65. self.log.warning('request object missing from response')
  66. return
  67. d=utils.getHash(r.request.headers,msgs.REQ)
  68. if not self.back_table.has_key(d):
  69. self.log.info('callback not found')
  70. return
  71. else:
  72. self.log.info('calling back')
  73. self.back_table[d](r)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement