Я пишу клиент Python для подключения к серверу, написанному на C, который отправляет статус в двоичной структуре. Я обернул структуру C с помощью SWIG, но мне нужно обрабатывать данные, возвращаемые из сокета, как структуру 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()