2017-08-09 52 views
1

我一直在使用gevent-websocket一段時間,但由於某種原因,它在OSX和Linux上都神祕破滅。 bitbucket和pypi上的人沒有迴應它就駁回了我的請求,就像在stackoverflow上的人一樣。我打算編寫自己的WebSocket實現,但我需要訪問管理原始數據發送和接收的原始連接對象(如來自套接字模塊的套接字對象)。我在哪裏可以找到這瓶裝?我在尋找的代碼可能看起來像這樣的:Python瓶發送原始響應套接字

@route("/websocket") 
def ws(): 
    raw_conn = ??? # socket object from socket module 
    # initialize websocket here, following protocols and then send messages 
    while True: 
     raw_conn.send(raw_conn.recv()) # Simple echo 
+0

你介意鏈接到你貼,關於如何SO問題'gevent-websocket'壞了?我很好奇看到細節。謝謝! –

+0

https://stackoverflow.com/questions/40876032/gevent-websocket-throws-protocolerror-when-socket-receive-is-called –

回答

0

一些代碼,我做的是,可以是有用的:

import abc 
import socket 


class Communication(metaclass=abc.ABCMeta): 
    def __init__(self, port): 
     self.port = port 
     self.connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

    def send_message(self, message): 
     self.my_connexion.send(message.encode()) 

    def wait_for_message(self, nb_characters = 1024): 
     message = self.my_connexion.recv(nb_characters)   
     return message.decode() 

    def close_connexion(self): 
     self.connexion.close() 


class Client(Communication): 
    def __init__(self, port): 
     Communication.__init__(self, port)  
     self.connexion.connect(("localhost", port)) 
     self.my_connexion = self.connexion 

class Server(Communication): 
    def __init__(self, port, failed_connexion_attempt_max = 1): 
     Communication.__init__(self, port) 

     self.connexion.bind(("", port)) 
     self.connexion.listen(failed_connexion_attempt_max) 
     self.my_connexion, address = self.connexion.accept() 

    def close_connexion(self): 
     self.client_connexion.close() 
     self.connexion.close() 
+0

這不能幫助瓶裝。 –