I’m learning twisted so I can integrate it with a blackjack pygame I have. When figuring out how to pass the data from one client to server and then to other clients I was trying to see how I could manipulate the strings that normally get printed to the terminal in each clients screen in this example:
When I say manipulate, I mean to change the datatype to list, int, tuple, etc. of the ‘data’, print information of it (type(data)), and have conditionals for any keywords.
from twisted.internet import stdio, reactor, protocol
from twisted.protocols import basic
import re
## when client receives data from server // the client script checks then blits ##
class DataForwardingProtocol(protocol.Protocol):
def __init__(self):
self.output = None
self.normalizeNewlines = False
def dataReceived(self,data):
if self.normalizeNewlines:
## see if data is == to secret ##
## this never returns True ? ##
if data == 'secret':
print "This line isn't secure"
else:
data = re.sub(r"(\r\n|\n)","\r\n",data)
if self.output:
if data == "secret":
print "This line isn't secure"
else:
## this will return the error message below ##
## so I'm very unsure of what is going on with 'data' ##
self.output.write(type(data))
class StdioProxyProtocol(DataForwardingProtocol):
def connectionMade(self):
inputForwarder = DataForwardingProtocol()
inputForwarder.output = self.transport
inputForwarder.normalizeNewlines = True
stdioWrapper = stdio.StandardIO(inputForwarder)
self.output = stdioWrapper
class StdioProxyFactory(protocol.ClientFactory):
protocol = StdioProxyProtocol
reactor.connectTCP('192.168.1.2', 6000, StdioProxyFactory())
reactor.run()
Returns:
Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 614, in _doReadOrWrite
why = selectable.doRead()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 203, in doRead
return self._dataReceived(data)
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 209, in _dataReceived
rval = self.protocol.dataReceived(data)
File "Downloads/trial.py", line 22, in dataReceived
self.output.write(type(data))
File "/usr/lib/python2.7/dist-packages/twisted/internet/_posixstdio.py", line 53, in write
self._writer.write(data)
File "/usr/lib/python2.7/dist-packages/twisted/internet/process.py", line 174, in write
abstract.FileDescriptor.write(self, data)
File "/usr/lib/python2.7/dist-packages/twisted/internet/abstract.py", line 335, in write
self._tempDataLen += len(data)
exceptions.TypeError: object of type 'type' has no len()
So I can never ‘check’ the ‘data’ and when I try to print out anything about the data or change its type I get an extensive error? Is there something obvious I’m missing or is this the wrong way to even go about it? If it helps here is the server script
When you call type() it returns the type of the object, and not a string representing the type of the object.
You could check the type like this instead:
The data you receive will not be a list, tuple or some other kind of object unless you serialize it. Check out the module
Pickleto see examples of how you can serialize objects!