简体   繁体   English

如何将python套接字流字符串值转换为float?

[英]How to convert python socket stream string value to float?

I'm starting to use sockets to send a file. 我开始使用套接字发送文件。 I learned I can send in 1024 "chunks" through the socket stream, and piece it back on the server side. 我了解到我可以通过套接字流发送1024个“块”,然后将其重新分配到服务器端。 I set the server to first get a string containing the size of the file it will receive, and as it reads to compare to see if the read is finished or not. 我将服务器设置为首先获取一个字符串,该字符串包含它将接收的文件的大小,并在读取时进行比较以查看读取是否完成。

Client code is: 客户代码为:

BUFFER_SIZE = 1024
limit = os.path.getsize("test.db") #4096 bytes
currentamt = BUFFER_SIZE
f = open("test.db", "rb")
l = f.read(BUFFER_SIZE)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(str(limit)) #first, send the limit
while(1):
  s.send(l)
  if currentamt >= limit:
    break
  l = f.read(BUFFER_SIZE)
  currentamt += BUFFER_SIZE
  print "still sending.."

s.close()
print "Done."

Server code is as such: 服务器代码是这样的:

while True:
  conn, addr = s.accept()
  print "Connection address: ", addr
  f = open('rec.db', 'wb')
  while 1:
    limit = conn.recv(20) #this will get your limit size
    limit = limit.rstrip()
    limit = float(limit) #error in question
    print limit
    l = conn.recv(BUFFER_SIZE) #get byte bufferred back
    while (1):
      if currentamt >= limit:
        f.write(l)
        break
      f.write(l)
      l = conn.recv(BUFFER_SIZE)
      currentamt += BUFFER_SIZE
  f.close() #close your file after you're done
  conn.close()

s.close()

If I change limit in server from 20 to 10, I am able to convert to a float, but then I get an error after the print statement! 如果我将服务器的限制从20更改为10,则可以转换为浮点数,但是在打印语句后出现错误! Also, the print limit does work before the float(limit) statement, which confuses me. 另外,打印限制确实在float(limit)语句之前起作用,这使我感到困惑。

TCP/IP guarantees that you get the right bytes in the right order. TCP / IP保证您以正确的顺序获得正确的字节。 It doesn't guarantee anything about the sizes of the chunks you receive. 它不能保证您收到的块的大小。 IOW, if you send 1024 bytes, 1024 bytes, 1024 bytes - then there's nothing to prevent TCP from chopping that into 512 bytes, 512 bytes, 512 bytes, 512 bytes, 512 bytes, 512 bytes - or even stranger things. IOW,如果您发送1024字节,1024字节,1024字节-则没有什么可以阻止TCP将其切为512字节,512字节,512字节,512字节,512字节,512字节-甚至更奇怪的事情。 It also reserves the right to aggregate chunks, to form 2048 bytes, 1024 bytes. 它还保留聚合块以形成2048字节,1024字节的权利。

To get around this, it's important to put a loop around your sends and recvs, or to use something that makes it easier to bufsock or twisted. 为了解决这个问题,重要的是在发送和接收过程中加一个循环,或者使用一些易于bufsock或扭曲的东西。 Personally, I think twisted makes this easier but a lot of other things harder, but maybe that's just because I wrote bufsock. 就个人而言,我认为扭曲使此操作更容易,但使其他许多事情变得更困难,但这也许只是因为我写了bufsock。

Bufsock is at http://stromberg.dnsalias.org/~strombrg/bufsock.html Bufsock位于http://stromberg.dnsalias.org/~strombrg/bufsock.html

In your specific case, I see you sending a length in ASCII, but I don't see you sending any sort of termination character or padding to a field width. 在您的特定情况下,我看到您发送的是ASCII长度,但看不到您发送任何形式的终止字符或填充字段宽度。 That'll reap havoc in the other side of the connection, where it appears to be assuming the length is always 20 characters long. 这会在连接的另一端造成严重破坏,这似乎是假设长度始终为20个字符。 To deal with this, you might: 为了解决这个问题,您可以:

s.send(str(limit) + '\n')

...but there's still the problem of missing loops. ...但是仍然存在缺少循环的问题。 Bufsock does the looping for you, you just have to remember to bufsock.flush() frequently (enough). Bufsock为您执行循环,您只需要记住经常(足够)bufsock.flush()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM