![]() |
PHP socket based communication with a python server
I've been tearing my hairs for the last 13 hours on this..
I have wrote a multi-threaded python scheduler, that listen on a socket for client commands. On each command received, it does something, write back something to the client and close the connection. I've developped this to monitor external commands, ran from a website, to be able to kill commands that freeze. The command is a gecko based web client, and sometimes, the library freeze on some pages. Nothing can stop it, except a kill -9 on the server. So, this server run that client in 1 new thread at each requests, and another thread monitor those client. 1 takes more than 1 minute to complete, a kill signal is sent to him. At the same time that the server, I have wrote a small python client (20 lines) that works very well, and I thought that it would be easy to port in php, but no luck until now. This is the python client: Code:
import socketI've translated it (with great inspiration taken from the php.net user manual) in php to: PHP Code:
My problem is that my script looks like being stuck in the while loop, where it tries to read the socket. If I put a length of 1, it goes through the loop without exiting it. If I put more than 1 as the wanted data length, it seems to hang like it was waiting for more to come. What seems strange to me, is that the server closes the connection after each answer sent, but php don't seems to see it This is what the server logs: Code:
~~~~Code:
w=webshot(id, self.context)Code:
telnet server 2727I never had problem like that with php and socket before, but it was always between 2 php processes. Does anyone here have any idea on how to solve this issue ? |
Ha !
I've found it.... I had to add a "\n" after each infos sent back. Php stop to read when he received a EOL character... So much time lost over such a litlle detail... |
this is a good resource to have, thanks for posting it and find the answer quickly. good to now about the EOL behavior
|
Also having a problem with socket based php communication with a python server
Hi
I am hoping someone has some insight into what is happening here. I am also writing a php script to connect to a python server. I have written a simple twisted telnet server (taken from the twisted tutorial) and a simple php client which writes to it. In the logs I can see the PHP client connecting to the server and disconnecting from the twisted server but none of the fwrite commands ever register in the server logs (or anywhere else on the twisted server). However I do not get an error in the PHP script, the fwrite function returns as though the function ran without errors. the twisted server PHP Code:
The php client PHP Code:
Does anyone have any idea as to where the data from the fwrite function is going? Thanks |
|
Thanks for the documentation
Thanks for pointing me to some more documentation to read up on (it is useful stuff). Unfortunately there is nothing here that fixes my problem directly. I'll look into to using the socket_create function to see if that fixes it.
Thanks |
fixed it
Aha,
Tony you where right about the socket_create stuff. After some reading up and re-writing and came up with this as a working php client :) PHP Code:
|
great! seems simple enough, I need to dive more into php socket programming, probably a project on that. hehe
|
| All times are GMT. The time now is 01:25 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0