gpt4 book ai didi

python - 扭转多个端口

转载 作者:行者123 更新时间:2023-11-30 23:36:37 26 4
gpt4 key购买 nike

我正在使用多端口应用程序,我的问题是关于我使用的解码器,让我用代码解释一下

这是我的 tac 文件,我在其中从配置文件中读取要使用的端口,该配置文件有用于监听客户端的端口的部分,还有接收到的数据的解码器,例如

[protocols]
keys: vt310,gt06,al900c

[al900c]
name:AL900C
port: 5010

[vt310]
name:VT310
port: 5020

[other]
name:OTHER
port: 5030

如您所见,我开始告诉解码器将在配置文件中搜索

import os, sys
import ConfigParser
from twisted.application import internet, service
from twisted.internet import protocol
from listener.TrackerServer import TrackerServer

PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(PROJECT_DIR)

path = None
config = ConfigParser.ConfigParser()
config.read('protocols.cfg')

application = service.Application("tracker")
for device in config.get('protocols', 'keys').split(','):
factory = protocol.ServerFactory()
factory.clients = []
factory.decoder = None
factory.protocol = TrackerServer
factory.devicer = config.get(device, 'name')
internet.TCPServer(int(config.get(device, 'port')), factory).setServiceParent(application)

我的服务器,每次启动连接时,服务器必须使用工厂创建时分配的解码器,

from twisted.internet import threads
from twisted.internet.protocol import Factory, Protocol

class TrackerServer(Protocol):
"""Tracker Twisted Protocol Class."""

def connectionMade(self):
"""ConnectionMade Twisted event."""
try:
decoderModule = __import__('listener.protocols.%sDecoder' % (self.factory.devicer, ), fromlist=['%sDecoder' % (self.factory.devicer, )])
decoderClass = getattr(decoderModule, '%sDecoder' % (self.factory.devicer, ))

self.factory.decoder = decoderClass()
print 'Connection made to', self.transport.getHost(), 'from', self.transport.getPeer()
self.factory.clients.append(self)
except ValueError:
print "Oops! Connection was not started"

def connectionLost(self, reason):
"""ConnectionLost Twisted event."""
self.factory.clients.remove(self)
print "Connection lost ", reason

def dataReceived(self, data):
"""DataReceived Twisted event."""
#try:
""" how to precess here a line for a specific client"""
self.sendResponse(self.factory.decoder.processDatagram(data))
#d = threads.deferToThread(self.factory.decoder.processDatagram(data ))
#d.addCallback(self.sendResponse)
#except ValueError:
# print "Oops! That was no valid data. Try again..."

def sendResponse (self, response):
self.transport.write(response)

问题是,至少我注意到,每次启动时,最后一个解码器都会启动,如您所见,在这种情况下,始终会使用 gt06,并且我需要使用解码器根据端口,这是我的日志,您可以注意到在 2013-05-04 11:29:56-0500,用于解码的类发生了变化

2013-05-08 09:09:05-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Loging] 78780d01086471700386307700af40fe0d0a 
2013-05-08 09:09:07-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.4255433333, lng: -71.5245183333
2013-05-08 09:09:09-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Status] 78780a13440604000200b1085f0d0a
2013-05-08 09:09:10-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.42506, lng: -71.5245766667
2013-05-08 09:09:17-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.4262866667, lng: -71.5243683333
2013-05-08 09:09:23-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.42436, lng: -71.5247866667
2013-05-08 09:09:25-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.4249, lng: -71.5246266667
2013-05-08 09:09:27-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.4271233333, lng: -71.524145
2013-05-08 09:09:38-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.428455, lng: -71.5238933333
2013-05-08 09:09:43-0500 [TrackerServer,1,190.113.208.135] [11742151 @ AL900CDecoder : Header is no valid] 2929b10007117421510caf0d
2013-05-08 09:09:47-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4291516667, lng: -71.5238566667
2013-05-08 09:09:59-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4293483333, lng: -71.5248666667
2013-05-08 09:10:08-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4294916667, lng: -71.525855
2013-05-08 09:10:18-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4296616667, lng: -71.526645
2013-05-08 09:10:27-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4297583333, lng: -71.5266416667
2013-05-08 09:10:37-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.430435, lng: -71.5265033333
2013-05-08 09:10:47-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.43122, lng: -71.5263833333
2013-05-08 09:10:57-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4317583333, lng: -71.52629
2013-05-08 09:11:07-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4323416667, lng: -71.5262116667
2013-05-08 09:11:17-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4328383333, lng: -71.5261283333
2013-05-08 09:11:27-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.43324, lng: -71.526085
2013-05-08 09:11:38-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.43384, lng: -71.5259933333
2013-05-08 09:11:47-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4342116667, lng: -71.5259316667
2013-05-08 09:11:56-0500 [TrackerServer,21,200.108.107.17] [32045214 @ AL900CDecoder : Header is no valid] 2929b10007320452140cca0d
2013-05-08 09:11:57-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Locating] lat: -16.4349533333, lng: -71.52572
2013-05-08 09:12:07-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Locating] lat: -16.43544, lng: -71.5255333333
2013-05-08 09:12:08-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Status] 78780a13440604000200c71eee0d0a
2013-05-08 09:12:17-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Locating] lat: -16.43572, lng: -71.52553
2013-05-08 09:12:18-0500 [TrackerServer,21,200.108.107.17] [32045214 @ AL900CDecoder : Header is no valid] 2929b10007320452140cca0d
2013-05-08 09:12:27-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Locating] lat: -16.4359733333, lng: -71.526235

最佳答案

您使用全局变量来指定某个端口上的服务器使用哪个解码器:

factory = protocol.ServerFactory()
factory.protocol = TrackerServer
factory.protocol.decoder = deco

由于全局只能有一个值,因此只能定义一个解码器。

此行之后:

factory.protocol = TrackerServer

factory.protocol 引用了 TrackerServer 引用的同一对象。所以该程序有效地执行了以下操作:

TrackerServer.decoder = deco

在循环内。很明显这不会达到您想要的效果,对吗?

作为替代方案,请在循环中尝试 factory.decoder = deco 并在协议(protocol)实现中尝试 self.factory.decoder (以替换 self.decoder)。

关于python - 扭转多个端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com