I would like to implement a TCP/IP network client application that sends requests to a Python SocketServer and expects responses in return. I have started out with the official Python SocketServer sample code:
server.py:
#!/usr/bin/env python
# encoding: utf-8
import SocketServer
class MyTCPHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        request  = self.rfile.readline().strip()
        print "RX [%s]: %s" % (self.client_address[0], request)
        response = self.processRequest(request)
        print "TX [%s]: %s" % (self.client_address[0], response)
        self.wfile.write(response)
    def processRequest(self, message):
        if   message == 'request type 01':
            return 'response type 01'
        elif message == 'request type 02':
            return 'response type 02'
if __name__ == "__main__":
    server = SocketServer.TCPServer(('localhost', 12345), MyTCPHandler)
    server.serve_forever()
client.py:
#!/usr/bin/env python
# encoding: utf-8
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    sock.connect(('127.0.0.1', 12345))
    data = 'request type 01'
    sent = sock.sendall(data + '\n')
    if sent == 0:
        raise RuntimeError("socket connection broken")
    received = sock.recv(1024)
    print "Sent:     {}".format(data)
    print "Received: {}".format(received)
    data = 'request type 02'
    sent = sock.sendall(data + '\n')
    if sent == 0:
        raise RuntimeError("socket connection broken")
    received = sock.recv(1024)
    print "Sent:     {}".format(data)
    print "Received: {}".format(received)
except Exception as e:
    print e
finally:
    sock.close()
server.py output:
RX [127.0.0.1]: request type 01
TX [127.0.0.1]: response type 01
client.py output:
Sent:     request type 01
Received: response type 01
[Errno 54] Connection reset by peer
What am doing wrong ? It seems the server is closing the connection. How can I make it stay open ?
Note: This is a follow-up question to C++/Qt: QTcpSocket won't write after reading
Update (after abarnert's answer):
What I take away from this is that SocketServer.StreamRequestHandler is not the most recent design and while it allows me to connect over a network, it doesn't really support me with all the TCP/IP-related aspects I need to take care of to implement robust communication.
This has been addressed in Python 3 with asyncio, but as the project lives in Python 2, that's not an option. I have therefore implemented the server and client described above in Twisted:
server.py:
#!/usr/bin/env python
# encoding: utf-8
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class SimpleProtocol(LineReceiver):
    def connectionMade(self):
        print 'connectionMade'
    # NOTE: lineReceived(...) doesn't seem to get called
    def dataReceived(self, data):
        print 'dataReceived'
        print 'RX: %s' % data
        if   data == 'request type 01':
            response = 'response type 01'
        elif data == 'request type 02':
            response = 'response type 02'
        else:
            response = 'unsupported request'
        print 'TX: %s' % response
        self.sendLine(response)
class SimpleProtocolFactory(Factory):
    def buildProtocol(self, addr):
        return SimpleProtocol()
reactor.listenTCP(12345, SimpleProtocolFactory(), interface='127.0.0.1')
reactor.run()
client.py:
#!/usr/bin/env python
# encoding: utf-8
from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
class SimpleClientProtocol(Protocol):
    def sendMessage(self, msg):
        print "[TX]: %s" % msg
        self.transport.write(msg)
def gotProtocol(p):
    p.sendMessage('request type 01')
    reactor.callLater(1, p.sendMessage, 'request type 02')
    reactor.callLater(2, p.transport.loseConnection)
point = TCP4ClientEndpoint(reactor, '127.0.0.1', 12345)
d = connectProtocol(point, SimpleClientProtocol())
d.addCallback(gotProtocol)
reactor.run()
The client doesn't close, but idles until CTRL+C. Twisted might take a while to get my head around, but for the job at hand, it clearly seems more reasonable to employ a tested and tried framework than to do all this groundwork myself.
NOTE: This is continued at Twisted XmlStream: How to connect to events?