websocket python爬虫_python爬虫请教一下,python怎么连接websocket

问题:python爬虫 请教一下,python怎么连接websocket

描述:

现在在做一个项目,需要用到websocket,需要python去连接websocket,但是不知道怎么用python连接websocket,找了好久没找到,求各位大佬帮忙~~解决方案1:

建议使用tornado,它支持websocket,知乎后端就是使用tornado构建的解决方案2:

感谢各位,百度了好久都找不到解决办法,楼上这位大神的应该可以用的,不过我看不太懂,感恩,上谷歌果然有收获,果断弃用百度

这是别人github上面的,可以用# install ws4py

# pip install ws4py

# easy_install ws4py

from ws4py.client.threadedclient import WebSocketClient

class DummyClient(WebSocketClient):

def opened(self):

self.send("www.baidu.com")

def closed(self, code, reason=None):

print "Closed down", code, reason

def received_message(self, m):

print m

if __name__ == '__main__':

try:

ws = DummyClient('ws://10.222.138.163:1889/websocket', protocols=['chat'])

ws.connect()

ws.run_forever()

except KeyboardInterrupt:

ws.close()解决方案3:

flask 使用 gevent-websocket + gunicorn 部署pip3 install gevent-websocket

pip3 install gunicorn

app.py demofrom geventwebsocket.handler import WebSocketHandler

from gevent.pywsgi import WSGIServer

app = Flask(__name__)

@app.route('/echo/')

def echo():

if request.environ.get('wsgi.websocket'):

ws = request.environ['wsgi.websocket']

while True:

msg = ws.receive()

ws.send(msg)

if __name__ == '__main__':

http_server = WSGIServer(('', 5000), app, handler_class=WebSocketHandler)

http_server.serve_forever()

使用 gunicorn 启动 指定用 gevent-websocketgunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker app:app

django 使用 Django-websocket

https://github.com/archever/p...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值