Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   PHP to Python? (http://www.programmingforums.org/showthread.php?t=14017)

SaturN Sep 21st, 2007 9:35 PM

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:

DaWei Sep 21st, 2007 10:01 PM

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.

Game_Ender Sep 22nd, 2007 12:09 AM

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).
:

  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('\\')


SaturN Sep 22nd, 2007 10:54 AM

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...

Game_Ender Sep 23rd, 2007 12:41 AM

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.

:

  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.

SaturN Sep 23rd, 2007 8:27 AM

That you very much! :)

free-zombie Sep 26th, 2007 4:06 PM

If you want to check whether any data can be read, the select module is what you're looking for.


All times are GMT -5. The time now is 11:27 AM.

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