【发布时间】:2014-08-08 19:08:41
【问题描述】:
我正在编写一个 Python 客户端来连接一个用 C 语言编写的服务器,该服务器以二进制结构发送状态。我已经用 SWIG 包装了 C 结构,但我需要将套接字返回的数据作为 C 结构处理。特别是,我想将传递给 dataReceived() 的数据转换为 iwrf_ui_task_operations 结构。
我是否需要编写(和 SWIG)一个传递“数据”并返回 iwrf_ui_task_operations 结构的辅助函数?
这是一个简单的测试程序:
from twisted.internet import reactor, protocol
import syscon_interface
class SysconClient(protocol.Protocol):
"""Once connected, receive messages from syscon."""
def connectionMade(self):
print "connectionMade"
def dataReceived(self, data):
"As soon as any data is received, write it out."
# this constructor does not accept 'data' as an argument :-(
to = syscon_interface.iwrf_ui_task_operations_t()
print "Server said:", data
def connectionLost(self, reason):
print "connection lost"
class SysconClientFactory(protocol.ClientFactory):
protocol = SysconClient
def clientConnectionFailed(self, connector, reason):
print "Connection failed - goodbye!"
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "Connection lost - goodbye!"
reactor.stop()
# this connects the protocol to a server running on port 2515
def main():
f = SysconClientFactory()
reactor.connectTCP("localhost", 2515, f)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()
【问题讨论】:
标签: python c sockets twisted swig