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.