我发现这个项目: http://code.google.com/p/standalonewebsocketserver/的WebSocket的服务器,但我需要实现在Python中的WebSocket客户端,更确切地说,我需要得到我的WebSocket服务器从XMPP一些命令。
Answer 1:
http://pypi.python.org/pypi/websocket-client/
可笑易于使用。
sudo pip install websocket-client
样本客户端代码:
#!/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()
示例服务器代码:
#!/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()
Answer 2:
高速公路对Python的一个很好的WebSocket客户端实现,以及一些很好的例子。 我测试了龙卷风的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()
Answer 3:
因为我一直在做该领域的一些研究,最近(一月,'12),最有希望的客户居然是: WebSocket的为Python 。 它支持正常的插座,你可以这样调用:
ws = EchoClient('http://localhost:9000/ws')
该client
可Threaded
或基于IOLoop
从龙卷风项目。 这将允许您创建一个多并发连接的客户端。 有用的,如果你想运行压力测试。
客户端也暴露了onmessage
, opened
和closed
方法。 (WebSocket的风格)。
Answer 4:
web2py中有comet_messaging.py,它采用了龙卷风的WebSockets看一个例子在这里: http://vimeo.com/18399381这里VIMEO。 COM / 18232653
Answer 5:
- 采取下看看回声客户http://code.google.com/p/pywebsocket/这是一个谷歌的项目。
- 在github上一个好的搜索是: https://github.com/search?type=Everything&language=python&q=websocket&repo=&langOverride=&x=14&y=29&start_value=1返回客户端和服务器。
- 布雷·泰勒也实现网络插座过龙卷风(蟒蛇)。 他的博客在: 在龙卷风网络套接字和客户端实现API显示在tornado.websocket在客户端的支持部分。
文章来源: Is there a WebSocket client implemented for python? [closed]