Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 21st, 2007, 10:35 PM   #1
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Smile PHP to Python?

Is there anyone that is able to convert this small php snippet
 fputs($sock,"\\status\\"); $gotfinal=False; $data="";
 socket_set_timeout($sock,0,200000);
 $starttime=Time();
 while(!($gotfinal==True||feof($sock))) {
  if(($buf=fgetc($sock))==FALSE) usleep(100);
  $data.=$buf;
  if(strpos($data,"final\\")!=False) $gotfinal=True;
  if((Time() - $starttime)>2) { 
    $gotfinal=True;
   }
 }
 fclose($sock);
 $chunks=split('[\]',$data);

to Python??
I've got

    global chunks
    s.send("\\status\\")
    final = False
    while(final != True):
        data = s.recv(1024)
        if (len(data) <= 0): sleep(1); data = data, s.recvfrom(1024)
        if(data.find("final\\") != False): final = True
    chunks = data.split('\\')

But as you'll notice tis is not really a good match !!
Thanks for anyone that is able to help!
It would be greatly appriciated!! :banana:
__________________
while me is alive:
	make(life,simple)
SaturN is offline   Reply With Quote
Old Sep 21st, 2007, 11:01 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Yeah, I can convert it. Understand that there is difference between translation/transliteration and implementing a good solution. Despite your sig, "make life simple", I think you need to learn to adapt to the good life. Hanging on the sugar tit is not generally considered to qualify, pleasant and unchallenging as it might be.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 22nd, 2007, 1:09 AM   #3
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Yeah, that code is messy and the intent is hard to follow. Use proper indentation (how did you manage not to in python?) and please explain the point of the code.

It appears to send "status\" over a socket then receive data (with some odd timeouts and sleeps thrown in) until it gets back "final\". Then it splits it based on the "\" character.

This is as close as I got (untested).
python Syntax (Toggle Plain Text)
  1. global chunks
  2.  
  3. # Request Status
  4. s.send("\\status\\")
  5.  
  6. data = ''
  7.  
  8. while True:
  9. # Attempt to get new data
  10. new_data = s.recv(1024)
  11.  
  12. # Drop out of the loop if we didn't get any new data
  13. if not new_data:
  14. break
  15.  
  16. data += new_data
  17.  
  18. # Check for termination string
  19. if data.find("final\\")
  20. break
  21.  
  22. s.close()
  23.  
  24. chunks = data.split('\\')
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Sep 22nd, 2007, 11:54 AM   #4
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Firstly the first code (PHP) Is not mine, but it is what I need for the code that I am writing in python, as for the indentation comment how that is relevant to it being poorly spaced on a forum I find irellevant within my code it works.

As for my sig, I can see sarcasm is wasted to your wise wit...
I came here asking for help not for a debate.

Your code that you have posted is pretty much a match to mine but thanks anyway..

The code should be simple to understand both in the PHP and Python.
It sends status to a socket, gets the data back from the socket, BUT I was hoping for a if no new data, wait and check again sort of thing, because at the moment some of the data that should be there is not! And from what I can interprit from the PHP if looks like if new data is not found it waits, then gets more, so maybe this is what my code was missing then split the data from the server in to chunks...

Sorry if it sounds like I am biting back at anyone in the above, but
"Hanging on the sugar tit is not generally considered to qualify" thought this was a 'Programming Forum' not a 'Lessons to Life Forum'....

def Update():
    global chunks
    s.send("\\status\\")
    final = False
    while(final != True):
        data = s.recv(1024)
        if(data.find("final\\") != False): final = True
    chunks = data.split('\\')

But I am unsure if this gets all the data from the server.
Cheers for anyone willing to help and not give bites...
__________________
while me is alive:
	make(life,simple)
SaturN is offline   Reply With Quote
Old Sep 23rd, 2007, 1:41 AM   #5
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
When I said indentation I was referring to the leaving of statements one line, no need to do so in python. It just increases the line noise to code ratio, which seemed high to my non-PHP trained eyes.

Quote:
Your code that you have posted is pretty much a match to mine but thanks anyway..
It is a corrected version of the code you posted. I did not run it but you appeared to be using "recvfrom" improperly and you are still overwriting the data you get from the server (data = s.recv(1024) throws out the previous received data). It will also give you an error when the socket returns None, as it does when it gets no data.

Here is a version of the code the implements the proper waiting semantics and is well commented.

python Syntax (Toggle Plain Text)
  1.  
  2. def Update(s):
  3. # Request Status
  4. s.send("\\status\\")
  5.  
  6. # Buffer to hold incoming data
  7. data = ''
  8.  
  9. start_time = time.time()
  10. while True:
  11. # Attempt to get new data
  12. new_data = s.recv(1024)
  13.  
  14. # Handle no new data case
  15. if not new_data:
  16. # Drop out its been two seconds
  17. if (time.time() - start_time) > 2:
  18. break
  19. else:
  20. # Sleep 100 micro seconds
  21. time.sleep(0.0001)
  22.  
  23. # Append new data to buffer
  24. data += new_data
  25.  
  26. # Check for termination string
  27. if data.find("final\\")
  28. break
  29.  
  30. s.close()
  31.  
  32. return data.split('\\')

I don't think the odd waiting code is needed, the socket timeout is should be enough. If you set the timeout to be 2 seconds the socket will block for 2 seconds to wait for new data.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Sep 23rd, 2007, 9:27 AM   #6
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
That you very much!
__________________
while me is alive:
	make(life,simple)
SaturN is offline   Reply With Quote
Old Sep 26th, 2007, 5:06 PM   #7
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
If you want to check whether any data can be read, the select module is what you're looking for.
free-zombie is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginers Python Tutorial Beegie_B Python 15 Jul 28th, 2006 12:43 PM
[tutorial] Python for programming beginners coldDeath Python 30 Dec 14th, 2005 12:35 PM
Convert Python script to C++ code clanotheduck Python 17 Sep 25th, 2005 9:55 AM
Advanced Python Tricks Arevos Python 19 Sep 24th, 2005 8:39 AM
Python - A Programmers Introduction coldDeath Python 17 Aug 19th, 2005 1:41 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC