2017-08-09 123 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

这不能帮助瓶装。 –