English 中文(简体)
Python socket not receiving anything
原标题:

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.

最佳回答

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.

问题回答

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?





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签