views:

196

answers:

2

I'm trying to receive a variable length stream from a camera with python, but get weird behaviour. This is Python 2.6.4 (r264:75706) on linux(Ubuntu 9.10)

The message is supposed to come with a static header followed by the size, and rest of the stream. here is the code

from socket import *
import array
import select

HOST = '169.254.0.10'
PORT = 10001
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
tcpCliSock.setblocking(0)

def dump(x):
    dfile = open('dump','w')
    dfile.write(x)
    dfile.close


data='I'
tcpCliSock.send(data)
tcpCliSock.shutdown(1)
ready_to_read, ready_to_write, in_error = select.select(
  [tcpCliSock], 
  [], 
  [], 
  30)
if ready_to_read == []:
 print "sokadens"
data=''
while len(data)<10:
 chunk = tcpCliSock.recv(1024)
 print 'recv\'d %d bites'%len(data)
 data=data+chunk
index=data.find('##IMJ')
if index == -1:
 dump(data)
 raise RuntimeError, "imahe get error"
datarr = array.array('B',data)
size=datarr[6]+datarr[7]<<8+datarr[8]<<16+datarr[9]<<24
ready_to_read, ready_to_write, in_error = select.select(
  [tcpCliSock], 
  [], 
  [], 
  30)
if ready_to_read == []:
 print "sokadens"
while len(data)<size:
 chunk = tcpCliSock.recv(1024)
 data=data+chunk 

outfile=open('resim.jpg','w')
outfile.write(data[10:])
outfile.close

tcpCliSock.close()

with this code I either get stuck in a "recv\'d 0 bites" loop(which happens rarely) or this:

`recv'd 0 bites`
Traceback (most recent call last):
  File "client.py", line 44, in <module>
    raise RuntimeError, "imahe get error"
RuntimeError: imahe get error

which is totally weird(receive 0 bytes but get out of the loop). The dumped data is erroneous, which is expected in that situation

Edit 1: the device is supposed to send a JPEG image, preceded by a 10-byte header. When(if) I get past the first loop, I need to check this header for correctness and size info. The program terminates with wrong data error, and the dump file is a bunch of binary garbage, so I have no Idea what I received at the end. I am pretty sure the device at the other side is trying to send the correct data.

Edit 2: I found the cause of the problem. You can consider this closed.

+1  A: 

You don't really know how many bytes you received, since your code is:

data=''
while len(data)<10:
        chunk = tcpCliSock.recv(1024)
        print 'recv\'d %d bites'%len(data)
        data=data+chunk

i.e., you're receiving bytes in chunk, but what you're printing is len(data) before you update data. So of course it will print 0 the first time, always -- then it will update data and exit if the chunk was at least 10 bytes.

This info is not sufficient to debug your problem, but printing len(chunk), and len(data) upon exiting the loop, can't hurt the attempt to understand what's going on. Also, what's in dump when you exit with the imahe get error message?

Alex Martelli
See edit1, the dump is useless for me in my current state. I will try to write a dummy server.
Atilla Filiz
A: 

Problem is resolved, interestingly shutdown(1) was causing the problem, the other side does not like http style shutdowns. There are also obvious typos and missing checks but they are not the problem.

Atilla Filiz