I have started to learn Python twisted. I have a basic requirement to map the request and response for a TCP client. The client can send any request anytime based on some condition and it expects a specific response for that request from a server. How I could map a request-response pair. A sample code of what I want. Also, the server could send an arbitrary message which does not map to any of client request.
class MSOProtocol(Protocol):
def __init__(self):
self.client_id = uuid4()
self.messagePair = collections.OrderedDict()
def connectionMade(self):
log.msg("Device connected to TSIF")
self.doOperation()
def connectionLost(self, reason):
log.msg('Device lost connection because {}'.format(reason))
def sendMessage(self, data):
log.msg('Device sending...str(msg)'.format(data))
self.transport.write(data)
def dataReceived(self, data):
log.msg('Device received {}'.format(data))
#how do I map the response with my request
def doOperation(self):
CONDITION = "rainy"
if condition == "sunny":
self.sendMessage("sunny weather")
class DeviceFactory(ClientFactory):
def buildProtocol(self, addr):
return MSOProtocol()
def main():
log.startLogging(sys.stdout)
reactor.connectTCP('127.0.0.1', 16000, DeviceFactory())
reactor.run()
if __name__ == '__main__':
main()
Related
Im creating an application to receive data from multiple iot devices
Each iot device has a socket server
so i need to create multi socket client
Im using Twisted library for create multiple socket client
Problem is that my application can't detect connection failure and it wont try for reconnection
Herer is my code:
import time
import requests
from twisted.internet.protocol import Protocol, ReconnectingClientFactory as rcf, connectionDone
from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.python import failure
client_list = [("device ip", device_port)]
endpoints = {}
class Client(Protocol):
"""
when receive a massage call this function
"""
def connectionMade(self):
self.transport.setTcpKeepAlive(True)
def dataReceived(self, data):
reactor.callInThread(self._dataReceived, data)
def _dataReceived(self, data):
if self.connected == 1:
data = data[:-1].decode("utf-8")
data = data.split('\n')
for tag in data:
reactor.callInThread(self._push_data, tag, self.transport.addr[0])
def _push_data(self, data, ip):
print("start pushing")
print(data[1:-1])
try:
pass
// send a web request
except Exception as xp:
print(xp, end="\n")
class ClientFactory(rcf):
def buildProtocol(self, addr):
print(addr, 'Connected.')
return Client()
if __name__ == "__main__":
for client in client_list:
endpoint = TCP4ClientEndpoint(reactor, client[0], client[1])
endpoints[client] = endpoint
endpoint.connect(ClientFactory())
reactor.run()
i writing some code for fix this problem but that's not worked
import time
import requests
from twisted.internet.protocol import Protocol, ReconnectingClientFactory as clf, connectionDone
from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.python import failure
client_list = [("device ip", device_port)]
endpoints = {}
class Client(Protocol):
"""
when receive a massage call this function
"""
def dataReceived(self, data):
reactor.callInThread(self._dataReceived, data)
def _dataReceived(self, data):
if self.connected == 1:
data = data[:-1].decode("utf-8")
data = data.split('\n')
for tag in data:
reactor.callInThread(self._push_data, tag, self.transport.addr[0])
def _push_data(self, data, ip):
print("start pushing")
print(data[1:-1])
try:
pass
// send a web request
except Exception as xp:
print(xp, end="\n")
def connectionLost(self, reason: failure.Failure = connectionDone):
try:
endpoints.pop(self.transport.addr[0])
except Exception as xp:
print(xp)
print(f"Protocol client {self.transport.addr[0]} connectionLost \n ", reason)
status = True
while status:
try:
endpoint = TCP4ClientEndpoint(reactor, self.transport.addr[0], 100)
endpoints[client] = endpoint
endpoint.connect(ClientFactory())
status = False
except Exception as xp:
print(xp)
class ClientFactory(clf):
def buildProtocol(self, addr):
print(addr, 'Connected.')
return Client()
def clientConnectionFailed(self, connector, reason):
print("can't start connect to the server")
print(reason)
clf.clientConnectionFailed(self, connector, reason)
def clientConnectionLost(self, connector, reason):
print(reason)
clf.clientConnectionLost(self, connector, reason)
if __name__ == "__main__":
for client in client_list:
endpoint = TCP4ClientEndpoint(reactor, client[0], client[1])
endpoints[client] = endpoint
endpoint.connect(ClientFactory())
reactor.run()
my question is how i can set an interval ping for check connection
I'm trying to simulate a situation where data is received by a server periodically. In my set up, I run one process that sets up the server and another process that sets up a bunch of clients (suffices to think of a single client). I have set up some of the code by putting together bits and pieces mostly from here. The server/clients communicate by sending messages using transport.write. First, the server tells the clients to start (this works fine AFAIK). The clients report back to the server as they make progress. What has me confused is that I only get these intermittent messages at the very end when the client is done. This could be a problem with buffer flush and I tried (unsuccessfully) things like This. Also, each message is pretty large and I tried sending the same message multiple times so the buffer would get cleared.
I suspect what I am seeing is a problem with returning the control to the transport but I can't figure out how to do away with it.
Any help with this or any other issues that jump up to you is much appreciated.
Server:
from twisted.internet import reactor, protocol
import time
import serverSideAnalysis
import pdb
#import bson, json, msgpack
import _pickle as pickle # I expect the users to authenticate and not
# do anything malicious.
PORT = 9000
NUM = 1
local_scratch="/local/scratch"
class Hub(protocol.Protocol):
def __init__(self,factory, clients, nclients):
self.clients = clients
self.nclients = nclients
self.factory = factory
self.dispatcher = serverSideAnalysis.ServerTalker(NUM, self,
local_scratch)
def connectionMade(self):
print("connected to user" , (self))
if len(self.clients) < self.nclients:
self.factory.clients.append(self)
else:
self.factory.clients[self.nclients] = self
if len(self.clients) == NUM:
val = input("Looks like everyone is here, shall we start? (Y/N)")
while (val.upper() != "Y"):
time.sleep(20)
val = input("Looks like everyone is here, shall we start??? (Y/N)")
message = pickle.dumps({"TASK": "INIT", "SUBTASK":"STORE"})
self.message(message) # This reaches the client as I had expected
def message(self, command):
for c in self.factory.clients:
c.transport.write(command)
def connectionLost(self, reason):
self.factory.clients.remove(self)
self.nclients -= 1
def dataReceived(self, data):
if len(self.clients) == NUM:
self.dispatcher.dispatch(data)
class PauseTransport(protocol.Protocol):
def makeConnection(self, transport):
transport.pauseProducing()
class HubFactory(protocol.Factory):
def __init__(self, num):
self.clients = set([])
self.nclients = 0
self.totConnections = num
def buildProtocol(self, addr):
print(self.nclients)
if self.nclients < self.totConnections:
self.nclients += 1
return Hub(self, self.clients, self.nclients)
protocol = PauseTransport()
protocol.factory = self
return protocol
factory = HubFactory(NUM)
reactor.listenTCP(PORT, factory)
factory.clients = []
reactor.run()
Client:
from twisted.internet import reactor, protocol
import time
import clientSideAnalysis
import sys
HOST = 'localhost'
PORT = 9000
local_scratch="/local/scratch"
class MyClient(protocol.Protocol):
def connectionMade(self):
print("connected!")
self.factory.clients.append(self)
print ("clients are ", self.factory.clients)
self.cdispatcher = clientSideAnalysis.ServerTalker(analysis_file_name, local_scratch, self)
def clientConnectionLost(self, reason):
#TODO send warning
self.factory.clients.remove(self)
def dataReceived(self, data): #This is the problematic part I think
self.cdispatcher.dispatch(data)
print("1 sent")
time.sleep(10)
self.cdispatcher.dispatch(data)
print("2 sent")
time.sleep(10)
self.cdispatcher.dispatch(data)
time.sleep(10)
def message(self, data):
self.transport.write(data)
class MyClientFactory(protocol.ClientFactory):
protocol = MyClient
if __name__=="__main__":
analysis_file_name = sys.argv[1]
factory = MyClientFactory()
reactor.connectTCP(HOST, PORT, factory)
factory.clients = []
reactor.run()
The last bit of relevant information about what the dispatchers do.
In both cases, they load the message that has arrived (a dictionary) and do a few computations based on the content. Every once in a while, they use the message method to communicate with thier current values.
Finally, I'm using python 3.6. and twisted 18.9.0
The way you return control to the reactor from a Protocol.dataReceived method is you return from that method. For example:
def dataReceived(self, data):
self.cdispatcher.dispatch(data)
print("1 sent")
If you want more work to happen after this, you have some options. If you want the work to happen after some amount of time has passed, use reactor.callLater. If you want the work to happen after it is dispatched to another thread, use twisted.internet.threads.deferToThread. If you want the work to happen in response to some other event (for example, data being received), put it in the callback that handles that event (for example, dataReceived).
I'm completely new to network programming and event driven events. However I was able to successfully implement a pub-sub scheme using a TCP connection between my machine (server) and client machines for testing (command line). However, I need to actually use a UNIX socket with Twisted.
I am getting the following error when running the code:
Unhandled error in Deferred
Here is my code for pub_sub.py:
"""
a networking implementation of PubSub using Twisted.
=============
PubSub Server
=============
A PubSub server listens for subscription requests and publish commands, and, when
published to, sends data to subscribers. All incoming and outgoing requests are
encoded in JSON.
A Subscribe request looks like this:
{
"command": "subscribe",
"topic": "hello"
}
A Publish request looks like this:
{
"command": "publish",
"topic": "hello",
"data": {
"world": "WORLD"
}
}
When the server receives a Publish request, it will send the 'data' object to all
subscribers of 'topic'.
"""
import argparse
import json
import logging
from collections import defaultdict
from twisted.internet import reactor
from twisted.python import log
from twisted.python.filepath import FilePath
from twisted.internet.endpoints import UNIXClientEndpoint, UNIXServerEndpoint, \
connectProtocol
from twisted.internet.protocol import Protocol, Factory
class PubSubProtocol(Protocol):
def __init__(self, topics):
self.topics = topics
self.subscribed_topic = None
def connectionLost(self, reason):
print("Connection lost: {}".format(reason))
if self.subscribed_topic:
self.topics[self.subscribed_topic].remove(self)
def dataReceived(self, data):
print("Data received: {}".format(data))
try:
request = json.loads(data)
except ValueError:
logging.debug("ValueError on deconding incoming data. "
"Data: {}".format(data), exc_info=True)
self.transport.loseConnection()
return
if request['command'] == 'subscribe':
self.handle_subscribe(request['topic'])
elif request['command'] == 'publish':
self.handle_publish(request['topic'], request['data'])
def handle_subscribe(self, topic):
print("Subscribed to topic: {}".format(topic))
self.topics[topic].add(self)
self.subscribed_topic = topic
def handle_publish(self, topic, data):
request = json.dumps(data)
for protocol in self.topics[topic]:
protocol.transport.write(request)
print("Publish sent for topic: {}".format(topic))
class PubSubFactory(Factory):
def __init__(self):
self.topics = defaultdict(set)
def buildProtocol(self, addr):
return PubSubProtocol(self.topics)
class PublisherProtocol(Protocol):
"""
Publish protocol for sending data to client, i.e. front-end web GUI.
"""
def __init__(self, topic, **kwargs):
self.topic = topic
self.kwargs = kwargs
def connectionMade(self):
request = json.dumps({
'command': 'publish',
'topic': self.topic,
'data': self.kwargs,
})
self.transport.write(request)
self.transport.loseConnection()
class SubscriberProtocol(Protocol):
"""
Subscriber protocol for client sending a request to subscribe to a specific
topic.
"""
def __init__(self, topic, callback):
self.topic = topic
self.callback = callback
def connectionMade(self):
request = json.dumps({
'command': 'subscribe',
'topic': self.topic,
})
self.transport.write(request)
def dataReceived(self, data):
kwargs = json.loads(data)
self.callback(**kwargs)
class PubSub(object):
def __init__(self, path='./.sock'):
self.path = FilePath(path)
self.reactor = reactor
def _make_connection(self, protocol):
endpoint = UNIXClientEndpoint(reactor, self.path)
connection = connectProtocol(endpoint, protocol)
def subscribe(self, topic, callback):
"""
Subscribe 'callback' callable to 'topic'.
"""
sub = SubscriberProtocol(topic, callback)
self._make_connection(sub)
def publish(self, topic, **kwargs):
"""
Publish 'kwargs' to 'topic', calling all callables subscribed to 'topic'
with the arguments specified in '**kwargs'.
"""
pub = PublisherProtocol(topic, **kwargs)
self._make_connection(pub)
def run(self):
"""
Convenience method to start the Twisted event loop.
"""
self.reactor.run()
def main():
path = FilePath("./.sock")
endpoint = UNIXServerEndpoint(reactor, path)
endpoint.listen(PubSubFactory())
reactor.run()
if __name__ == '__main__':
main()
Any help would be greatly appreciated on what I am doing wrong.
Thank you,
Brian
You appear to be running your software on Windows. Alas, UNIX sockets are not available on Windows. If you want to use UNIX sockets, you need to use a more POSIX-ish environment - Linux, *BSD, OS X, etc.
I am developing a chat application using Twisted framework in Python. However, sometimes the messages are delivered with a huge delay of upto 2 minutes from server to client.
Any idea what could be the reason for this delay ? Is this related to slower network ?
class MultiClientEcho(Protocol):
def __init__(self, factory):
self.factory = factory
def connectionMade(self):
ip = self.transport.getPeer().host
def dataReceived(self, data):
print "data is " + data
client.transport.write(data)
class MultiClientEchoFactory(Factory):
def __init__(self):
self.clients = []
def buildProtocol(self, addr):
return MultiClientEcho(self)
def main():
reactor.listenTCP(8000, MultiClientEchoFactory())
reactor.run()
if __name__ == '__main__':
main()
Thanks
I have this simple Twisted Client which connects to a Twisted server & queries an index.
If you see fn. connectionMade() in class SpellClient, the query is hard-coded. Did that for testing purposes. How would one pass this query from outside to this class?
The code -
from twisted.internet import reactor
from twisted.internet import protocol
# a client protocol
class SpellClient(protocol.Protocol):
"""Once connected, send a message, then print the result."""
def connectionMade(self):
query = 'abased'
self.transport.write(query)
def dataReceived(self, data):
"As soon as any data is received, write it back."
print "Server said:", data
self.transport.loseConnection()
def connectionLost(self, reason):
print "connection lost"
class SpellFactory(protocol.ClientFactory):
protocol = SpellClient
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 runing on port 8000
def main():
f = SpellFactory()
reactor.connectTCP("localhost", 8090, f)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()
Protocols, like SpellClient, have access to their factory as self.factory.
...so there would be a number of ways to do this, but one way would be to create another method on SpellFactory, such as setQuery, and then access that from the client...
#...in SpellFactory:
def setQuery(self, query):
self.query = query
#...and in SpellClient:
def connectionMade(self):
self.transport.write(self.factory.query)
...so in main:
f = SpellFactory()
f.setQuery('some query')
...
...or you could just create an _init_ method for SpellFactory, and pass it in there.