如果在接收数据包时检测到某些字节,python套接字会丢弃数据包
我有这个简单的 python 脚本,
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.sendto(b'ok',('123.123.123.123',1234))
recv,addr = sock.recvfrom(128) # This line is receiving packet?
当数据包处于下行流时,如何处理数据包? 我的意思是,如果检测到某些字节,例如 b'\xfc'
,我想忽略数据包
,我可以在完全接收到缓冲区/数据之后实现它:
if b'\xfc' in recv: sock.close()
但这个弱点是我需要等待数据/缓冲区完全接收,如果目标服务器回复大数据包怎么办?这效率不高,我需要在关闭之前完全收到目标服务器的回复。
如果我的问题很难理解,就理解这个类比吧。假设你想跟我说话,我就开始听你说话。当你说话的时候,我正在处理你的每一个字符,如果有某个字符比如你告诉我的字母's',我会在你发给我这个字符后突然闭上耳朵。
I have this simple python script
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.sendto(b'ok',('123.123.123.123',1234))
recv,addr = sock.recvfrom(128) # This line is receiving packet?
How do I handle packet while it's in down streaming?
I mean I want ignore packet if certain bytes detected for example b'\xfc'
I can achieve it AFTER the buffer/data completely received with doing this:
if b'\xfc' in recv: sock.close()
But this weakness is I need to wait data/buffer completely received, what if destination server reply with big packet size? This will not efficient, I need to fully received destination server reply before close.
If my question is hard to understand, just understand this analogy. Suppose you want talk to me, and I'm starting listening you. While you are talking, I'm processing every your character, if there's certain character for example letter 's' you told to me, I will close my ear suddenly exactly after you send me such character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论