def on_metadata(self): if "stream_id" in self.metadata: pr = PipelineRouter(self.db, self.metadata["stream_id"]) server = yield tornado.gen.Task(pr.get_server) url = pr.get_updates_url(server) # websocket.enableTrace(True) cache_hash = url + "?stream_id=" + self.metadata["stream_id"] if cache_hash in self.settings["ws_cache"]: self.client_receiver = self.settings["ws_cache"][cache_hash] else: # print "vyt" client = websocket.create_connection(url) # client = yield tornado.gen.Task(websocket.create_connection, url) # print "at" client.send(json.dumps(self.metadata)) # print client self.client_receiver = ClientWSReceiver(cache_hash, client) self.client_receiver.start() self.settings["ws_cache"][cache_hash] = self.client_receiver self.client_receiver.add_handler(self) print(self.metadata) if "action" in self.metadata: print('send ', self.metadata) self.client_receiver.client.send(self.metadata)
def solve(solver, message, corr_id): if type(message)==str: pass else: message = message.decode('utf-8') out = solvers[solver](message) ws = websocket.create_connection("ws://localhost:" + portnum + "/mathserver?" + solver + "?" + corr_id) ws.send(out) ws.close()
def emitData(): global tprec # Délai nécessaire pour que le serveur de Websocket ait le temps de démarrer delay(5000) digitalWrite(13,HIGH) # Démarrage du client Websocket (indispensable d'avoir toujours un client connecté) try: websocket.create_connection("ws://" + get_ip_address('eth0') + ":" + "9090" + "/ws") except: pass try: websocket.create_connection("ws://" + get_ip_address('wlan0') + ":" + "9090" + "/ws") except: pass tprec = time.time() while not noLoop: loop() # appelle fonction loop sans fin
def ThreadWorker(self, stopThreadEvent): logger.info('Starting') wsURL = 'ws://music.james:6680/mopidy/ws' try: ws = websocket.create_connection(wsURL) while not stopThreadEvent.isSet(): response = ws.recv() logger.info(response) except: logger.info('Error')
def solve(solver, message, corr_id): if type(message) == str: pass else: message = message.decode('utf-8') out = solvers[solver](message) ws = websocket.create_connection("ws://localhost:" + portnum + "/mathserver?" + solver + "?" + corr_id) ws.send(out) ws.close()
def on_metadata(self): if "stream_id" in self.metadata: pr = PipelineRouter(self.db, self.metadata["stream_id"]) server = yield tornado.gen.Task(pr.get_server) url = pr.get_updates_url(server) # websocket.enableTrace(True) self.client = websocket.create_connection(url) self.client.send(json.dumps(self.metadata)) self.client_receiver = ClientWSReceiver(self) self.client_receiver.start()
def connect(self): print("trying to connect") try: self.ws = yield websocket.create_connection( 'wss://streamer.cryptocompare.com/socket.io/?transport=websocket', timeout=5 ) self.ws.send('SubAdd', {'subs': ['0~Poloniex~BTC~USD']}) # self.ws.wait() except Exception as e: print("connection error", e) else: print("connected") # subscription = ['5~CCCAGG~BTC~USD', '5~CCCAGG~ETH~USD'] # self.ws.write_message('SubAdd', {'subs': subscription}) self.run()
def on_metadata(self): if "stream_id" in self.metadata: stream = yield motor.Op( self.db.streams.find_one, {"_id": ObjectId(self.metadata["stream_id"])}, { "reencoding": 1, "_id": 0 }) if stream['reencoding']: pr = PipelineRouter(self.db, self.metadata["stream_id"]) server = yield tornado.gen.Task(pr.get_server) url = pr.get_live_url(server) # websocket.enableTrace(True) self.client = websocket.create_connection(url) self.client.send(json.dumps(self.metadata)) else: self.close()
class wshandle(tornado.websocket.WebSocketHandler): websocket.enableTrace(False) ws = websocket.create_connection("ws://169.254.6.158:9090") #make websocket connection with Galileo board def open(self): #when some one connects with this system print "connection open..." def on_message(self, m): print "msg: " wshandle.ws.send("#789#") self.make_sentence(m.lower()) #convert sentance in lower case def on_close(self): print 'connection closed...' def check_origin(self, origin): return True def make_sentence(self, string): pst = PunktSentenceTokenizer(string) t = pst.tokenize(string) word = nltk.word_tokenize(t[0]) #here we chunking sentance into word tagged = nltk.pos_tag(word) #here each word is tagged means it is noud, pronoun, etc... is recognized print tagged chunkGram = r"""WRB:{<WRB.?>*<WP>*<WDT>?}""" #REGEXP for detecting wh question chunkParser = nltk.RegexpParser(chunkGram) #differentiate wh question chunked = chunkParser.parse(tagged) for subtree in chunked.subtrees( ): #getting each word this will gives the output in tree form if subtree.label() == 'WRB': # for only wh question for j in subtree.leaves(): f = 0 final = "" final += j[0] chunk = r"""VB: {<VBZ>*<VBP>?}""" #here we are detecting type of wording and arranging it to proper place cp = nltk.RegexpParser(chunk) word = nltk.word_tokenize(t[0]) tagged = nltk.pos_tag(word) ch = cp.parse(tagged) flg = 0 for subtree in ch.subtrees(): if subtree.label() == 'VB': for j in subtree.leaves(): final += " " + j[0] flg = 1 break if flg == 0: final += " is" chunk = r"""PRP: {<PRP.?>?}""" cp = nltk.RegexpParser(chunk) ch = cp.parse(tagged) for subtree in ch.subtrees(): if subtree.label() == 'PRP': for j in subtree.leaves(): final += " " + j[0] chunk = r"""PRP: {<JJ.?>?}""" cp = nltk.RegexpParser(chunk) ch = cp.parse(tagged) for subtree in ch.subtrees(): if subtree.label() == 'PRP': for j in subtree.leaves(): final += " " + j[0] chunk = r"""PRP: {<RB.?>?}""" cp = nltk.RegexpParser(chunk) ch = cp.parse(tagged) for subtree in ch.subtrees(): if subtree.label() == 'PRP': for j in subtree.leaves(): final += " " + j[0] chunk = r"""PRP: {<VB.?>?}""" cp = nltk.RegexpParser(chunk) ch = cp.parse(tagged) for subtree in ch.subtrees(): if subtree.label() == 'PRP': for j in subtree.leaves(): final += " " + j[0] chunk = r"""NN: {<NN.?>?}""" cp = nltk.RegexpParser(chunk) ch = cp.parse(tagged) for subtree in ch.subtrees(): if subtree.label() == 'NN': for j in subtree.leaves(): if f == 0: final += " " + j[0] f = 1 else: final += " of " + j[0] f = 0 print final final_string = self.grammar( final ) #sending generated sentance to ginger grammer for correcting grammar print final_string wshandle.ws.send( final_string.upper()) #sending final sentance to board return chunkGram = r"""NN:{<PRP.?>*<NN.?>?}""" #same thing like wh question is here for simple present tence sentance chunkParser = nltk.RegexpParser(chunkGram) chunked = chunkParser.parse(tagged) for subtree in chunked.subtrees(): if subtree.label() == 'NN': for j in subtree.leaves(): f = 0 w = nltk.word_tokenize(string) w.remove(j[0]) final = "" final += " " + j[0] chunk = r"""VB: {<VBP>*<VBZ>*<VB>*<VB.?>*<MD.?>?}""" cp = nltk.RegexpParser(chunk) word = nltk.word_tokenize(t[0]) tagged = nltk.pos_tag(word) ch = cp.parse(tagged) flg = 0 for subtree in ch.subtrees(): if subtree.label() == 'VB': for j in subtree.leaves(): w.remove(j[0]) final += " " + j[0] flg = 1 break if flg == 0: final += " is" chunk = r"""PRP: {<PRP.?>?}""" cp = nltk.RegexpParser(chunk) ch = cp.parse(nltk.pos_tag(w)) for subtree in ch.subtrees(): if subtree.label() == 'PRP': for j in subtree.leaves(): final += " " + j[0] w.remove(j[0]) chunk = r"""NN: {<NN.?>?}""" cp = nltk.RegexpParser(chunk) ch = cp.parse(nltk.pos_tag(w)) for subtree in ch.subtrees(): if subtree.label() == 'NN': for j in subtree.leaves(): if f == 0: final += " " + j[0] f = 1 else: final += " of " + j[0] w.remove(j[0]) f = 0 for wrd in w: final += " " + wrd print final final_string = self.grammar(final) print final_string wshandle.ws.send(final_string.upper()) return #this is for correcting grammar of generated sentence. #this method create url for ginger api #param: string #return: url #working: generates url with api key, input data and many other param #example: url = get_ginger_url("what is you name"): def get_ginger_url(self, text): API_KEY = "6ae0c3a0-afdc-4532-a810-82ded0054236" scheme = "http" netloc = "services.gingersoftware.com" path = "/Ginger/correct/json/GingerTheText" params = "" query = urllib.urlencode([("lang", "US"), ("clientVersion", "2.0"), ("apiKey", API_KEY), ("text", text)]) fragment = "" return (urlparse.urlunparse( (scheme, netloc, path, params, query, fragment))) #param: string #return: json object #working: get response from generated url #example: json = get_ginger_result("what is you name"): def get_ginger_result(self, text): #this method fatchs json result url = self.get_ginger_url(text) try: response = urllib.urlopen(url) except HTTPError as e: print("HTTP Error:", e.code) quit() except URLError as e: print("URL Error:", e.reason) quit() except IOError, (errno, strerror): print("I/O error (%s): %s" % (errno, strerror)) quit try: result = json.loads(response.read().decode('utf-8')) except ValueError: print("Value Error: Invalid server response.") quit() return (result)
def srv_data(url, token, data, kind): """Serve data to RainbowAlga""" ws = websocket.create_connection(url) message = {'token': token, 'data': data, 'kind': kind} ws.send(json.dumps(message)) ws.close()
import grobid config = json.loads(open(sys.argv[1]).read()) grobid = grobid.Grobid("http://localhost:10810") s = slack.Slack(config['slack']['token']) im = s.open_im('U04QRTZ3X') channel = im['channel']['id'] response = s.rtm_start() ws_url = response['url'] ws = websocket.create_connection(ws_url) id = 1 while True: message_string = ws.recv() if message_string != "": message = json.loads(message_string) #print message if "type" in message and message["type"] == "message": if "subtype" in message and message["subtype"] == "file_share": filetype = message["file"]["filetype"] file_url = message["file"]["url_private_download"] file_id = message["file"]["id"]
if __name__ == "__main__": import sys if len(sys.argv) == 1: print("server or client") elif sys.argv[1] == "server": wsservers = [] for i in ws_settings.SERVERS: mod = __import__(i +".ws") wsservers.append((mod.ws.SERVER_URL, mod.ws.WebSocket)) application = tornado.web.Application(wsservers) application.listen(8080) print("runserver: " + ws_settings.URL) print("run apps:\n - {}".format("\n - ".join(ws_settings.SERVERS))) tornado.ioloop.IOLoop.instance().start() elif sys.argv[1] == "client": THREADS = [] for i in ws_settings.CLIENTS: mod = __import__(i +".ws") client_data = __import__(i +".data.account") header = {"Type" : "receiver"} header.update(client_data.data.account.DATA) ws = websocket.create_connection("{0}{1}".format( ws_settings.URL, mod.ws.CLIENT_URL ), header=header) thread = mod.ws.WSThread(ws) thread.start() print(" === connect {} === ".format(i))
def srv_data(url, token, data, kind): """Serve data to RainbowAlga""" ws = websocket.create_connection(url) message = {'token': token, 'data': data, 'kind': kind} ws.send(pd.io.json.dumps(message)) ws.close()