socket server Python 服务端如何主动发送消息? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
woshichuanqilz
V2EX    Python

socket server Python 服务端如何主动发送消息?

  •  
  •   woshichuanqilz 2019-05-23 11:56:40 +08:00 3006 次点击
    这是一个创建于 2406 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我查看了这里的资料, 看到 server 发送消息都是在 handle 里面也就是说要先有客户端有消息发送过来才会做出对应的发送消息的动作, 那么如果我想在服务端主动的发送消息给客户端这个应该怎么做? https://docs.python.org/3/library/socketserver.html#socketserver.BaseServer.server_bind

    这个是实例代码, 这个 handle 里面处理了消息发送的过程。

    import socketserver class MyUDPHandler(socketserver.BaseRequestHandler): """ This class works similar to the TCP handler class, except that self.request consists of a pair of data and client socket, and since there is no connection the client address must be given explicitly when sending data back via sendto(). """ def handle(self): data = self.request[0].strip() socket = self.request[1] print("{} wrote:".format(self.client_address[0])) print(data) socket.sendto(data.upper(), self.client_address) if __name__ == "__main__": HOST, PORT = "localhost", 9999 with socketserver.UDPServer((HOST, PORT), MyUDPHandler) as server: server.serve_forever() 
    3 条回复    2019-05-23 14:11:44 +08:00
    prim
        1
    prim  
       2019-05-23 12:15:54 +08:00
    建议你看看 TCP
    如果非要用 UDP 的话,你就可以在第一次收到这个 client address 的时候把这个 socket 和 address 存起来
    你想在哪里主动发数据的话就再用前面记录下来的 socket 和 address

    如果你是想在 client 没有发包过来的时候就发包给他
    那么你把这个 client 实现成你代码里面的 UDPServer 就好了,这样你就能提前这个 client address
    xomix
        2
    xomix  
       2019-05-23 13:00:51 +08:00
    这是一个很常见的问题,百度关键字 udp 打洞。
    至于怎么做你自己百度吧。
    julyclyde
        3
    julyclyde  
       2019-05-23 14:11:44 +08:00
    socketserver 里对于 TCP 协议,所谓“消息”是指一个连接
    对于 UDP 协议,当然必须先收到消息才能确定角色。如果没收到就发,那叫客户端不叫服务器
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5521 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 55ms UTC 06:53 PVG 14:53 LAX 22:53 JFK 01:53
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86