0

I have to write 7231 bytes into a file using python script. In a client-server program, my python script act like client and it received 7231 bytes from server. If I check in TCP-Dump, its shows complete data. But when I try to write into a file; I am missing the content.

My script:

   def SendOnce(self, req='/gpsData=1',method="GET"):
       conn = httplib.HTTPConnection(self.proxy)
       self.Logresponse("\nConnection Open\n<br />")
       conn.request(method,req)
       Log="\nRequest Send: %s\n<br \>\n" %req
       self.Logresponse(Log)
       response = conn.getresponse()
       Log = "\nResponse Code: %s\n<br \>\n" %response.status
       self.Logresponse(Log)
       Log = "\nSarav -- Get Header: %s \n version= %s <br \>\n"           %(response.msg,response.version)
       self.Logresponse(Log)
       if (response.status==200):
           Log = response.read()
           self.Logresponse(Log)
       conn.close()
       self.Logresponse("\nConnection Close\n<br \>")
       return response

this "self.Logresponse(Log)" is writing into file. If i receive 1023 bytes, its writing full content into that. Please help me out how to write complete data. Note: I am writing Hexa Format data.

6
  • 2
    Please use the code tags (the {} icon) to format your code. Otherwise we have to guess at its structure since indentation is semantically significant. Commented Dec 1, 2011 at 12:37
  • 5
    Since when is 7K a "huge" amount of data? Commented Dec 1, 2011 at 12:41
  • 3
    Probably we will need to see Logresponse to help with this problem. Commented Dec 1, 2011 at 12:41
  • 1
    Are you flushing the stream after the write? Please post the code for def Logresponse too. Commented Dec 1, 2011 at 12:42
  • @Wooble I think the question is rather, 'How long has it been since 7K was a "huge" amount of data?' ;) Commented Dec 1, 2011 at 12:54

2 Answers 2

3

First of all, 7231 bytes is not exactly huge...

With the limited info you gave, I would guess that you might have forgotten to take the OS's write buffer into account. You probably try to read the file before all the content was written to it.

Python generally uses the system's standard buffer (you can change that). You can decrease that buffer, or force a flush yourself.

Sign up to request clarification or add additional context in comments.

4 Comments

Using context management on files is also a good idea.
the Logresponse Code: { def Logresponse(self,response): message=response logfile=open(".//log//BSRLog.html",'a') logfile.write(message) logfile.close() }
Hm, not good. The close() already does an implicit flush(). This does not mean that the data must be actually physically on the disk - but the next open() to read should in fact return the whole thing... Strange. Could you try to open with bufsize set to 0 in Logresponse? i.e. open(".//log//BSRLog.html",'a', 0)
@rwos: Thanks for response. Yes! i did the same. but getting the same result. Data is not written completely in BSRLog.html file. Actually the file size shows 7K bytes but still half of the data content i am missing.
0

I'm just guessing, it might be that the .read() function doesn't return all data in one chunk; can you try to modify the inner part like this:

if (response.status==200):
     while 1:
         Log = response.read()
         if not Log:
             break
         self.Logresponse(Log)

2 Comments

This above code is giving syntax error. Where to fit this break statement. Its says syntax err: break is out of loop
Check indentation; the 'break' breaks out of the while loop; this is correct code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.