111

このプロジェクトを見つけました: http://code.google.com/p/standalonewebsocketserver/ WebSocket サーバー用ですが、Python で WebSocket クライアントを実装する必要があります。より正確には、WebSocket サーバーで XMPP からいくつかのコマンドを受け取る必要があります。

4

5 に答える 5

190

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()
于 2012-06-19T16:45:46.237 に答える
23

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()
于 2012-01-18T17:01:06.193 に答える
10

最近 ('12 年 1 月) その分野で少し調査を行っているので、実際に最も有望なクライアントはWebSocket for Python です。次のように呼び出すことができる通常のソケットをサポートします。

ws = EchoClient('http://localhost:9000/ws')

Tornadoプロジェクトをベースにすることも、ベースにするclientこともできます。これにより、複数同時接続クライアントを作成できます。ストレス テストを実行する場合に便利です。ThreadedIOLoop

onmessageクライアントは、 、openedおよびclosedメソッドも公開します。(WebSocket スタイル)。

于 2012-01-09T13:19:07.210 に答える
0
  1. http://code.google.com/p/pywebsocket/のエコー クライアントを見てください。これは 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 ソケットとクライアント実装 API は、クライアント側のサポート セクションのtornado.websocketに示されています。
于 2011-01-04T15:15:10.187 に答える
-1

web2py には comet_messaging.py があり、これは Websocket に Tornado を使用しています: http://vimeo.com/18399381と vimeo の例を見てください。com / 18232653

于 2011-01-04T16:31:08.033 に答える