- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经成功连接到 usb 调制解调器,客户端可以通过 tcp 连接到我的 reactor.listenTCP,从调制解调器收到的数据将被发送回客户端。我想从客户端获取数据并将其发送到调制解调器..我正在努力让它工作。任何帮助将不胜感激!代码:
from twisted.internet import win32eventreactor
win32eventreactor.install()
from twisted.internet import reactor
from twisted.internet.serialport import SerialPort
from twisted.internet.protocol import Protocol, Factory
from twisted.python import log
import sys
log.startLogging(sys.stdout)
client_list = []#TCP clients connecting to me
class USBClient(Protocol):
def connectionFailed(self):
print "Connection Failed:", self
reactor.stop()
def connectionMade(self):
print 'Connected to USB modem'
USBClient.sendLine(self, 'AT\r\n')
def dataReceived(self, data):
print "Data received", repr(data)
print "Data received! with %d bytes!" % len(data)
#check & perhaps modify response and return to client
for cli in client_list:
cli.notifyClient(data)
pass
def lineReceived(self, line):
print "Line received", repr(line)
def sendLine(self, cmd):
print cmd
self.transport.write(cmd + "\r\n")
def outReceived(self, data):
print "outReceived! with %d bytes!" % len(data)
self.data = self.data + data
class CommandRx(Protocol):
def connectionMade(self):
print 'Connection received from tcp..'
client_list.append(self)
def dataReceived(self, data):
print 'Command receive', repr(data)
#Build command, if ok, send to serial port
#????
def connectionLost(self, reason):
print 'Connection lost', reason
if self in client_list:
print "Removing " + str(self)
client_list.remove(self)
def notifyClient(self, data):
self.transport.write(data)
class CommandRxFactory(Factory):
protocol = CommandRx
def __init__(self):
client_list = []
if __name__ == '__main__':
reactor.listenTCP(8000, CommandRxFactory())
SerialPort(USBClient(), 'COM8', reactor, baudrate='19200')
reactor.run()
最佳答案
你的问题不是关于扭曲,而是关于 python。阅读此常见问题条目:
How do I make input on one connection result in output on another?
问题是,如果您想在串行连接协议(protocol)中将内容发送到 TCP 连接的客户端,只需向协议(protocol)传递对工厂的引用,这样您就可以使用该引用来建立桥接。
下面是一些大致执行此操作的示例代码:
class USBClient(Protocol):
def __init__(self, network):
self.network = network
def dataReceived(self, data):
print "Data received", repr(data)
#check & perhaps modify response and return to client
self.network.notifyAll(data)
#...
class CommandRx(Protocol):
def connectionMade(self):
self.factory.client_list.append(self)
def connectionLost(self, reason):
if self in self.factory.client_list:
self.factory.client_list.remove(self)
class CommandRxFactory(Factory):
protocol = CommandRx
def __init__(self):
self.client_list = []
def notifyAll(self, data):
for cli in self.client_list:
cli.transport.write(data)
初始化时,传递引用:
tcpfactory = CommandRxFactory()
reactor.listenTCP(8000, tcpfactory)
SerialPort(USBClient(tcpfactory), 'COM8', reactor, baudrate='19200')
reactor.run()
关于Python Twisted 从 TCP 接收命令写入串行设备返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4715340/
我正在尝试发送 RFC5424 中定义的结构化数据从 Twisted 到系统日志。我在 Ubuntu 上,系统日志实现是 rsyslog。 我正在使用“twistd --syslog”运行扭曲的应用程
摘要:无法自动向 Twisted 的检修孔发出命令。寻求解决方案/建议。 Twisted 有一个很棒的功能,称为 manhole。它允许用户通过 ssh 连接到当前正在运行的 Twisted 服务器并
我正在尝试发送 RFC5424 中定义的结构化数据从 Twisted 到系统日志。我在 Ubuntu 上,系统日志实现是 rsyslog。 我正在使用“twistd --syslog”运行扭曲的应用程
我正在尝试使用扭曲的 spawnProcess 启动一个进程,并使用 psutil 每 5 秒记录一次资源使用情况。首先,我尝试使用以下代码获取生成的进程的可执行文件名称: #!/usr/bin/py
Azure 网站能否托管 Twisted 应用程序?例如像这样的东西: from twisted.internet import reactor from twisted.web import ser
有没有办法限制 Twisted http 客户端的下载速率?如果没有,在 Twisted 中实现此类客户端的最简单方法是什么? 最佳答案 Twisted 中的流控制最常使用 IProducer.pau
我的基于 Twisted 的客户端循环发送 UDP 数据包。因此,我正在使用 DatagramProtocol 类。这是来源: #!/usr/bin/python # -*- coding: utf-
raven 有很多集成,包括 python 日志记录。一方面,twisted 不使用python 的日志记录。而另一方面,在twisted 中,raven 并没有直接的整合。 那么在基于扭曲的设置中使
有人告诉我,Twisted基于库Ampoule是创建在不同计算机上执行的进程池的好方法。然而,没有相关文档,Ampoule 的示例也没有说明这一点。 我对类似于 stdlib multiprocess
Twisted 最近推出了一个新的日志记录模块:twisted.logger。我已经通读了文档 [1],但我一直无法找到设置实际日志级别的位置。关于日志观察者[2] 的部分建议,如果您显式配置自己的
我想问一个关于如何关闭扭曲连接的问题 RPC . 我知道有人问过类似的问题,但似乎没有回答我的问题。 我正在做一些基本的连接,如下图所示: cfactory = pb.PBClientFactory(
我正在使用 Twisted 编写代码,但在为我的扭曲互联网延迟变量想出一个合理的变量名时遇到了麻烦。这是我的候选人: d :太通用,太短,违反了 pylint 规则 C0103。 def :与内置函数
我正在为 Web 服务器的静态部分使用 Twisted Web static.File 资源。 对于开发,我希望能够添加新文件或修改当前静态文件,而无需重新启动 Twisted 网络服务器。 我在 g
我想使用 Twisted 重建现有应用程序的通信部分。这个应用程序确实从客户端向服务器发送数据,只是这样,服务器不发送任何东西。 我如何使用 Twisted 的事件驱动概念来实现这一目标?我目前使用
我开始考虑通过扩展当前的 Twisted FTP 来实现显式 FTP。 大部分代码都很简单,实现 AUTH、PBSZ、PROT 很容易,我得到了一个有效的安全控制 channel 。 我的问题是数据通
我想实现 WebRTC 对等连接。我不知道如何设置 ICE 服务器或应该使用什么工具。任何人都可以给我建议吗?非常感谢。 最佳答案 实际上你不需要编写自己的 STUN/TURN 服务器 有很多现成的解
正如标题所示,ProcessProtocol 类上的这两个函数有什么区别?关于什么时候应该使用一种而不是另一种的文档有点稀疏? 我最好寻找一些可以证明这一点的用例示例。 最佳答案 我猜文档在这一点上有
使用下面的代码,我似乎可以相当轻松地使用 multiprocessing.reduction 在子进程中重建套接字.. import socket,os import multiprocessing
我正在尝试在 python 上使用 twisted 编写服务器。这是我文件的头部: from twisted.internet.protocol import Factory, Protocol fr
我使用 Twisted 制作了一个简单的 http 服务器,它发送 Content-Type: multipart/x-mixed-replace header 。我正在使用它来测试我想设置为接受长期
我是一名优秀的程序员,十分优秀!