![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
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) |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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)
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
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) |
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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:
Here is a version of the code the implements the proper waiting semantics and is well commented. python Syntax (Toggle Plain Text)
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 |
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
That you very much!
![]()
__________________
while me is alive: make(life,simple) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Beginers Python Tutorial | Beegie_B | Python | 15 | Jul 28th, 2006 11:43 AM |
| [tutorial] Python for programming beginners | coldDeath | Python | 30 | Dec 14th, 2005 11:35 AM |
| Convert Python script to C++ code | clanotheduck | Python | 17 | Sep 25th, 2005 8:55 AM |
| Advanced Python Tricks | Arevos | Python | 19 | Sep 24th, 2005 7:39 AM |
| Python - A Programmers Introduction | coldDeath | Python | 17 | Aug 19th, 2005 12:41 PM |