2013-03-28 16 views
0

打印解壓的花車我嘗試從一個UDP數據報得到花車和打印他們驗證:Python不能從網絡

import socket 
from struct import * 

socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
socket.bind(('127.0.0.1', 2416)) 
msg = bytearray(4*1024) 
f1 = 0.0 
f2 = 0.0 
f3 = 0.0 
while True: 
    nBytes = socket.recv_into(msg) 
    print('%d bytes received' % nBytes) 
    (f1) = unpack_from('!f', msg, 0) 
    (f2) = unpack_from('!f', msg, 4) 
    (f3) = unpack_from('!f', msg, 8) 
    print('%f, %f, %f received' % (f1, f2, f3)) 

下引發錯誤:

$ python Server.py 
12 bytes received 
Traceback (most recent call last): 
    File "Server.py", line 13, in <module> 
    print('%f, %f, %f received' % (f1, f2, f3)) 
TypeError: a float is required 

預期輸出是1.2, 3.4, 5.6 received

語境:

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32 

的信息,Java的UDP發送方(客戶端):

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 
import java.nio.ByteBuffer; 

public class Client { 

    public static void main(String[] args) throws Exception { 
     try(DatagramSocket clientSocket = new DatagramSocket()) { 
     InetAddress target = InetAddress.getByName("localhost"); 
     ByteBuffer msg = ByteBuffer.allocate(4*1024); 
     for(;;) { 
      msg.clear(); 
      msg.putFloat( 1.20f); 
      msg.putFloat( 3.40f); 
      msg.putFloat( 5.60f); 
      msg.putDouble( 7.80 ); 
      msg.putDouble( 9.10 ); 
      msg.putDouble(11.120); 
      msg.flip(); 
      clientSocket.send(
       new DatagramPacket(msg.array(), msg.limit(), target, 2416)); 
      Thread.sleep(2000); 
     } 
     } 
    } 
} 
+0

什麼是F1,F2和F3的數據類型? –

+0

這是我第一次使用Python,如何打印數據類型? – Aubin

+0

unpack返回一個元組,我必須用'[0]' – Aubin

回答

2

這條線的問題是:

(f1) = unpack_from('!f', msg, 0) 

嘗試

(f1,) = unpack_from('!f', msg, 0) 

請注意額外的逗號。對於其他兩條線也是如此。如上所述,unpack_from返回元組。 (f1)不是一個元組,它是一個單值。 (f1,)是包含一個元素的元組。

+0

獲得第一部分額外的逗號意味着f1是元組的一部分,而不是元組本身? – Aubin

+0

類別。 'X'是一個變量。 '(X)'也是一個變量。 '(X,Y,Z)'是一個大小爲三的元組。 '(X,)'是一個大小爲1的元組。 –

+0

我明白了,您的解決方案比我的表現力更強,我會更改我的代碼。 – Aubin

0

這個程序運作良好:

import socket 
from struct import * 

socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
socket.bind(('127.0.0.1', 2416)) 
msg = bytearray(4*1024) 
while True: 
    nBytes = socket.recv_into(msg) 
    print('%d bytes received' % nBytes) 
    (f1,) = unpack_from('!f', msg, 0) 
    (f2,) = unpack_from('!f', msg, 4) 
    (f3,) = unpack_from('!f', msg, 8) 
    (d1,) = unpack_from('!d', msg, 12) 
    (d2,) = unpack_from('!d', msg, 20) 
    (d3,) = unpack_from('!d', msg, 28) 
    print(f1, f2, f3, d1, d2, d3) 
    print(type(f1), type(d1)) 

和打印:

$ python Server.py 
36 bytes received 
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12 
<class 'float'> <class 'float'> 
36 bytes received 
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12 
<class 'float'> <class 'float'> 
36 bytes received 
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12 
<class 'float'> <class 'float'>