从套接字流接收数据 [英] Receive data from socket stream

查看:58
本文介绍了从套接字流接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下从套接字流接收数据的标准方法

(使用socket.socket.recv())。当你知道你将要收到的数据量是多少时,或者你将收到数据直到

远程对等关闭时,这很简单连接。但是我不确定哪个是

最好的方式来接收来自流的未确定长度的消息

在您希望保持打开的连接中。到现在为止,我已经做了这个小技巧:


data = client.recv(256)

new = data

而len(新)== 256:

new = client.recv(256)

data + = new


在大多数情况下效果很好。但它显然容易出错。如果

客户端发送*正好*二百五十六个字节怎么办?它将继续等待循环内的数据。是否真的有更好的和/ b $ b b标准方式,或者这是最好的吗?


对不起,如果这是一个偏离主题和更多相关联网,

但我还是在使用Python。


谢谢,

塞巴斯蒂安

解决方案

s0****@gmail.com 写道:


我想要求从套接字流接收数据的标准方法

(使用socket.socket.recv())。当你知道你将要收到的数据量是多少时,或者你将收到数据直到

远程对等关闭时,这很简单连接。但是我不确定哪个是

最好的方式来接收来自流的未确定长度的消息

在您希望保持打开的连接中。到现在为止,我已经做了这个小技巧:


data = client.recv(256)

new = data

而len(新)== 256:

new = client.recv(256)

data + = new


在大多数情况下效果很好。但它显然容易出错。如果

客户端发送*正好*二百五十六个字节怎么办?它将继续等待循环内的数据。是否真的有更好的和/ b $ b b标准方式,或者这是最好的吗?


对不起,如果这是一个偏离主题和更多相关联网,

但我还是在使用Python。



你可以通过一个客户端和服务器都同意的协议来解决这个问题,这样客户就可以知道读取多少内容了服务器。

有很多方法可以做到这一点,所有这些都取决于您想要传输的

类型的数据以及用于何种目的。


-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

美国加利福尼亚州圣何塞市&& 37 18 N 121 57 W&& AIM,Y!M erikmaxfrancis

在最后的选择中,士兵的包装并不像

囚犯的链条那么沉重。 - 德怀特·艾森豪威尔,1890-1969


4月25日,5:52 * pm,Eri​​k Max Francis< m ... @ alcyone.comwrote:


s0s ... @ gmail.com写道:


我想要求标准方式接收来自套接字流的数据

(使用socket.socket.recv())。当你知道你将要收到的数据量是多少时,或者你将收到数据直到

远程对等关闭时,这很简单连接。但是我不确定哪个是

最好的方式来接收来自流的未确定长度的消息

在您希望保持打开的连接中。到目前为止,我一直在做这个小技巧:


data = client.recv(256)

new =数据

而len(新)== 256:

* * new = client.recv(256)

* * data + = new


在大多数情况下效果很好。但它显然容易出错。如果

客户端发送*正好*二百五十六个字节怎么办?它将继续等待循环内的数据。是否真的有更好的和/ b $ b b标准方式,或者这是最好的吗?


很抱歉,如果这有点偏离主题并且与网络更相关,

但我正在使用Python无论如何。



你可以通过一个客户端和服务器都同意的协议来解决这个问题,这样客户就可以知道要读取多少内容了服务器。

有很多方法可以做到这一点,所有这些都取决于您想要传输的

类型的数据以及用于何种目的。


-

Erik Max Francis&& m ... @ alcyone.com&& http://www.alcyone.com / max /

*美国加利福尼亚州圣何塞市&& 37 18 N 121 57 W&& AIM,Y!M erikmaxfrancis

* *在最后的选择中,士兵的包装并不像

* *囚犯的链条那么沉重。 - Dwight D. Eisenhower,1890-1969



因此,在HTTP客户端/服务器中,我必须查看Content-Length

标题?


4月25日,7:39 * pm,s0s ... @ gmail.com写道:


我想问一下从套接字流接收数据的标准方法

(使用socket.socket.recv())。当你知道你将要收到的数据量是多少时,或者你将收到数据直到

远程对等关闭时,这很简单连接。但是我不确定哪个是

最好的方式来接收来自流的未确定长度的消息

在您希望保持打开的连接中。到现在为止,我已经做了这个小技巧:


data = client.recv(256)

new = data

而len(新)== 256:

* * new = client.recv(256)

* * data + =新的


在大多数情况下效果很好。但它显然容易出错。如果

客户端发送*正好*二百五十六个字节怎么办?它将继续等待循环内的数据。是否真的有更好的和/ b $ b b标准方式,或者这是最好的吗?


对不起,如果这是一个偏离主题和更多相关联网,

但我还是在使用Python。


谢谢,

Sebastian


done = False

剩余=''''

完成时==错误:

data = client.recv(256)

完成,剩余=进程(剩余+数据)


PS:你确定你不应该使用RPC或SOAP?


I wanted to ask for standard ways to receive data from a socket stream
(with socket.socket.recv()). It''s simple when you know the amount of
data that you''re going to receive, or when you''ll receive data until
the remote peer closes the connection. But I''m not sure which is the
best way to receive a message with undetermined length from a stream
in a connection that you expect to remain open. Until now, I''ve been
doing this little trick:

data = client.recv(256)
new = data
while len(new) == 256:
new = client.recv(256)
data += new

That works well in most cases. But it''s obviously error-prone. What if
the client sent *exactly* two hundred and fifty six bytes? It would
keep waiting for data inside the loop. Is there really a better and
standard way, or is this as best as it gets?

Sorry if this is a little off-topic and more related to networking,
but I''m using Python anyway.

Thanks,
Sebastian

解决方案

s0****@gmail.com wrote:

I wanted to ask for standard ways to receive data from a socket stream
(with socket.socket.recv()). It''s simple when you know the amount of
data that you''re going to receive, or when you''ll receive data until
the remote peer closes the connection. But I''m not sure which is the
best way to receive a message with undetermined length from a stream
in a connection that you expect to remain open. Until now, I''ve been
doing this little trick:

data = client.recv(256)
new = data
while len(new) == 256:
new = client.recv(256)
data += new

That works well in most cases. But it''s obviously error-prone. What if
the client sent *exactly* two hundred and fifty six bytes? It would
keep waiting for data inside the loop. Is there really a better and
standard way, or is this as best as it gets?

Sorry if this is a little off-topic and more related to networking,
but I''m using Python anyway.

You solve this by having a protocol that the client and server both
agree on, so that the client knows how much to read from the server.
There are any number of ways of doing this, all of which depend on the
kind of data you want to transfer and for what purpose.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
In the final choice a solider''s pack is not so heavy a burden as a
prisoner''s chains. -- Dwight D. Eisenhower, 1890-1969


On Apr 25, 5:52*pm, Erik Max Francis <m...@alcyone.comwrote:

s0s...@gmail.com wrote:

I wanted to ask for standard ways to receive data from a socket stream
(with socket.socket.recv()). It''s simple when you know the amount of
data that you''re going to receive, or when you''ll receive data until
the remote peer closes the connection. But I''m not sure which is the
best way to receive a message with undetermined length from a stream
in a connection that you expect to remain open. Until now, I''ve been
doing this little trick:

data = client.recv(256)
new = data
while len(new) == 256:
* * new = client.recv(256)
* * data += new

That works well in most cases. But it''s obviously error-prone. What if
the client sent *exactly* two hundred and fifty six bytes? It would
keep waiting for data inside the loop. Is there really a better and
standard way, or is this as best as it gets?

Sorry if this is a little off-topic and more related to networking,
but I''m using Python anyway.


You solve this by having a protocol that the client and server both
agree on, so that the client knows how much to read from the server.
There are any number of ways of doing this, all of which depend on the
kind of data you want to transfer and for what purpose.

--
Erik Max Francis && m...@alcyone.com &&http://www.alcyone.com/max/
* San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
* *In the final choice a solider''s pack is not so heavy a burden as a
* * prisoner''s chains. -- Dwight D. Eisenhower, 1890-1969

So, in an HTTP client/server, I''d had to look in a Content-Length
header?


On Apr 25, 7:39*pm, s0s...@gmail.com wrote:

I wanted to ask for standard ways to receive data from a socket stream
(with socket.socket.recv()). It''s simple when you know the amount of
data that you''re going to receive, or when you''ll receive data until
the remote peer closes the connection. But I''m not sure which is the
best way to receive a message with undetermined length from a stream
in a connection that you expect to remain open. Until now, I''ve been
doing this little trick:

data = client.recv(256)
new = data
while len(new) == 256:
* * new = client.recv(256)
* * data += new

That works well in most cases. But it''s obviously error-prone. What if
the client sent *exactly* two hundred and fifty six bytes? It would
keep waiting for data inside the loop. Is there really a better and
standard way, or is this as best as it gets?

Sorry if this is a little off-topic and more related to networking,
but I''m using Python anyway.

Thanks,
Sebastian


done = False
remaining = ''''
while done == False:
data = client.recv(256)
done, remaining = process(remaining + data)

PS: are you sure you shouldn''t be using RPC or SOAP ?


这篇关于从套接字流接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆