このプロジェクトを見つけました: http://code.google.com/p/standalonewebsocketserver/ WebSocket サーバー用ですが、Python で WebSocket クライアントを実装する必要があります。より正確には、WebSocket サーバーで XMPP からいくつかのコマンドを受け取る必要があります。
5 に答える
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 "Receiving..."
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()
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()
最近 ('12 年 1 月) その分野で少し調査を行っているので、実際に最も有望なクライアントはWebSocket for Python です。次のように呼び出すことができる通常のソケットをサポートします。
ws = EchoClient('http://localhost:9000/ws')
Tornadoプロジェクトをベースにすることも、ベースにするclient
こともできます。これにより、複数同時接続クライアントを作成できます。ストレス テストを実行する場合に便利です。Threaded
IOLoop
onmessage
クライアントは、 、opened
およびclosed
メソッドも公開します。(WebSocket スタイル)。
- http://code.google.com/p/pywebsocket/のエコー クライアントを見てください。これは Google プロジェクトです。
- github での適切な検索: https://github.com/search?type=Everything&language=python&q=websocket&repo=&langOverride=&x=14&y=29&start_value=1クライアントとサーバーを返します。
- Bret Taylor は、Tornado (Python) を介した Web ソケットも実装しました。彼のブログ投稿: Tornado の Web ソケットとクライアント実装 API は、クライアント側のサポート セクションのtornado.websocketに示されています。
web2py には comet_messaging.py があり、これは Websocket に Tornado を使用しています: http://vimeo.com/18399381と vimeo の例を見てください。com / 18232653