python asyncio tcp server_Python:SocketServer意外关闭TCP连接

我想实现一个TCP/IP网络客户机应用程序,它向Python SocketServer发送请求并期望得到响应。我从官方的PythonSocketServer 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:

^{pr2}$

server.py输出:RX [127.0.0.1]: request type 01

TX [127.0.0.1]: response type 01

client.py输出:Sent: request type 01

Received: response type 01

[Errno 54] Connection reset by peer

你做错什么了?服务器似乎正在关闭连接。我怎么才能让它一直开着呢?在

我从中得到的是SocketServer.StreamRequestHandler不是最新的设计,虽然它允许我通过网络连接,但它并不能真正支持我实现健壮通信所需的所有与TCP/IP相关的方面。在

Python3中已经用asyncio解决了这一问题,但是由于项目位于Python2中,所以这不是一个选项。因此,我实现了上述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()

客户端不会关闭,但会一直闲置到CTRL+C。Twisted可能需要一段时间才能弄明白,但是对于手头的工作来说,使用一个经过测试和尝试的框架显然比自己做所有这些基础工作更为合理。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值