1

I'm trying to get a basic Python echo server running and to have a PHP script interface with it. The server receives data successfully and throws no exceptions when writing, but all attempts to read that data in the client time out. Here's the code.

Server:

import socketserver
import sys

class EchoRequestHandler(socketserver.StreamRequestHandler):

    def handle(self):
        data = self.rfile.readline()
        print('{} sent: {}'.format(self.client_address[0], data))
        try:
            self.wfile.write(data)
        except Exception as e:
            print(repr(e))
        sys.stdout.flush()


if __name__ == '__main__':
    socketserver.TCPServer.allow_reuse_address = True
    server = socketserver.TCPServer(('localhost', 34447), EchoRequestHandler)
    server.serve_forever()

Client:

$stream = stream_socket_client("tcp://127.0.0.1:34447");
fwrite($stream, 'Test message.');
stream_set_timeout($stream, 2);
$result = stream_get_contents($stream);
// Tried fread as well; either way I get an empty result after timeout.
fclose($stream);

The server successfully prints the received data, but no matter how I try to read a response to the client, it just times out and gets nothing. I assume I must be missing something obvious but I can't figure it out. Thanks in advance.

Larry
  • 11
  • 1
  • Does php block during the request, ie is it async or sync. It doesn't look async but this one of those topics that full of pitfalls in php. – ArtisticPhoenix Jul 26 '15 at 04:02
  • It's blocking, as far as I understand. When I tried to do a read with non-blocking specified, it immediately returned an empty string. – Larry Jul 26 '15 at 04:46
  • ok, I was wondering if that was the issue, because you wouldn't be able to read the output if it didn't block and wait for the response. You might be better off rolling out one of the pre-built message Queue systems like RabbitMq I have used before and is quite easy to implement. And has a ton of really good/usefull features. – ArtisticPhoenix Jul 26 '15 at 04:52

0 Answers0