Reputation: 894
I discovered twisted half-way through developing a network application. I had a working Client and Server using raw socket communication, but have opted to use Twisted in order to handle multiple connections asynchronously on the Server side. The twisted server works fine with the following raw-socket client class connecting to it:
class Client:
def __init__(self, backend, port=22220):
import socket
self.role_buf = 4096
self.buf = 1024
self.addr = (backend, port)
self.TCPSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.TCPSock.connect(self.addr)
def getRoleList(self):
self.TCPSock.send("RLP")
data = self.TCPSock.recv(self.role_buf)
return data.split("|")
def getServer(self, roleindex=0):
self.TCPSock.send(roleindex)
ip = self.TCPSock.recv(self.buf)
self.TCPSock.close()
return ip
What I'd like to do is refactor the client code using Twisted's clean socket and reactor abstraction. I'd like to be able to instantiate the client class in my Qt-based client GUI (Using PyQt) to gather event-driven (asynchronous) input from the user by calling getRoleList()
and getserver()
from the PyQt based client code. I don't want to use the PyQt socket/reactor implementation, I'd like to use a single library for my network communication.
Is this possible, and if so how should I do it? I think it should be, but having spent quite a while researching and reading twisted documentation, including these excellent tutorials I can't seem to glean from them exactly how to go about implementing a solution. Maybe I'm doing something silly and I need my hand slapped?
Typical Server/Client Interaction:
The server is always listening on port 22220, and when it receives a "role list request" from the client, it pushes it's (dynamic) list of role choices to the client. The client then displays these role choices to the user in a GUI, the user selects the single role they would like, then the client sends that request back to the server. The server then responds with data appropriate to the user's specific selection.
Upvotes: 0
Views: 987
Reputation: 17246
Your best bet for quick Twisted client and server info is to look at the Twisted How To docs:
http://twistedmatrix.com/documents/current/core/howto/index.html
In particular: Writing a TCP Server (which you seem to have working)
and: Writing a TCP Client
After you have your head wrapped around that, get the third party qtreactor from here:
https://github.com/ghtdak/qtreactor
and use that to integrate Qt and twisted together in your client. This reactor allows PyQt and Twisted to run side-by-side in the same application without blocking each other.
Upvotes: 2