是否为python实现了WebSocket客户端?

die*_*us9 92 python xmpp client-server websocket

我发现这个项目:http://code.google.com/p/standalonewebsocketserver/用于websocket服务器,但我需要在python中实现websocket客户端,更确切地说,我需要从我的websocket服务器中的xmpp接收一些命令.

Bry*_*unt 153

http://pypi.python.org/pypi/websocket-client/

非常容易使用.

 sudo pip install websocket-client
Run Code Online (Sandbox Code Playgroud)

示例客户端代码:

#!/usr/bin/python

from websocket import create_connection
ws = create_connection("ws://localhost:8080/websocket")
print "Sending 'Hello, World'..."
ws.send("Hello, World")
print "Sent"
print "Reeiving..."
result =  ws.recv()
print "Received '%s'" % result
ws.close()
Run Code Online (Sandbox Code Playgroud)

示例服务器代码:

#!/usr/bin/python
import websocket
import thread
import time

def on_message(ws, message):
    print message

def on_error(ws, error):
    print error

def on_close(ws):
    print "### closed ###"

def on_open(ws):
    def run(*args):
        for i in range(30000):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print "thread terminating..."
    thread.start_new_thread(run, ())


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://echo.websocket.org/",
                                on_message = on_message,
                                on_error = on_error,
                                on_close = on_close)
    ws.on_open = on_open

    ws.run_forever()
Run Code Online (Sandbox Code Playgroud)

  • @SSHT这个'pip install`在windows上运行正常!我正在使用ActivePython 2.7并运行`pip install websocket-client`它就可以了.唯一的问题是`python`与cygwin python冲突,所以我必须明确地运行`/ cygdrive/C/Python27/python`来获得ActivePython (4认同)
  • 天哪,不要使用“sudo”。使用“--user”。 (3认同)

chr*_*ick 20

Autobahn有一个很好的用于Python的websocket客户端实现以及一些很好的例子.我使用Tornado WebSocket服务器测试了以下内容并且它可以工作.

from twisted.internet import reactor
from autobahn.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS


class EchoClientProtocol(WebSocketClientProtocol):

   def sendHello(self):
      self.sendMessage("Hello, world!")

   def onOpen(self):
      self.sendHello()

   def onMessage(self, msg, binary):
      print "Got echo: " + msg
      reactor.callLater(1, self.sendHello)


if __name__ == '__main__':

   factory = WebSocketClientFactory("ws://localhost:9000")
   factory.protocol = EchoClientProtocol
   connectWS(factory)
   reactor.run()
Run Code Online (Sandbox Code Playgroud)

  • `from autobahn.twisted.websocket import WebSocketClientFactory,WebSocketClientProtocol,connectWS`是新版本的高速公路的正确导入.http://stackoverflow.com/questions/21381454/autobahn-cannot-import-name-error (2认同)

kid*_*ouk 10

由于我最近在该领域进行了一些研究(1月,12年),最有希望的客户实际上是:WebSocket for Python.它支持一个普通的套接字,你可以这样调用:

ws = EchoClient('http://localhost:9000/ws')
Run Code Online (Sandbox Code Playgroud)

clientThreaded或基于IOLoop龙卷风项目.这将允许您创建多并发连接客户端.如果你想进行压力测试,这很有用.

客户端也暴露了onmessage,openedclosed方法.(WebSocket风格).


sw.*_*sw. 0

  1. 看看http://code.google.com/p/pywebsocket/下的echo客户端,它是一个Google项目。
  2. github 中的一个很好的搜索是:https://github.com/search?type=Everything&language=python&q=websocket&repo=&langOverride=&x=14&y=29&start_value=1它返回客户端和服务器。
  3. Bret Taylor 还通过 Tornado (Python) 实现了 Web 套接字。他的博客文章:Tornado 中的 Web Sockets和客户端实现 API 显示在客户端支持部分的Tornado.websocket中。