【问题标题】:Twisted data being read/written by client?客户端正在读取/写入扭曲的数据?
【发布时间】:2023-03-07 06:35:01
【问题描述】:

我正在学习扭曲,所以我可以将它与我拥有的二十一点 pygame 集成。在弄清楚如何将数据从一个客户端传递到服务器然后再传递给其他客户端时,我试图了解如何操作通常在本示例中的每个客户端屏幕中打印到终端的字符串:

当我说操作时,我的意思是将'data'的数据类型更改为list、int、tuple等,打印它的信息(type(data)),并为任何关键字设置条件。

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()

返回:

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()

所以我永远无法“检查”“数据”,而当我尝试打印有关数据的任何内容或更改其类型时,我会遇到广泛的错误?有什么明显的我遗漏了,或者这是错误的方法吗?如果有帮助,这里是server 脚本

【问题讨论】:

    标签: python client twisted


    【解决方案1】:

    当您调用 type() 时,它返回对象的类型,而不是表示对象类型的字符串。

    你可以像这样检查类型:

    >>> aString = 'abc'
    >>> anInt = 123
    >>> type(aString) is str
    True
    >>> type(aString) is int
    False
    >>> type(anInt) is str
    False
    >>> type(anInt) is int
    True
    

    您收到的数据不会是列表、元组或其他类型的对象,除非您对其进行序列化。查看模块Pickle,查看如何序列化对象的示例!

    【讨论】:

    • 这不是在命令行上执行此操作,而是我将尝试将其作为str(type(data)) 之类的字符串传回
    • J.F.塞巴斯蒂安我不确定你的意思,但是,在我上面的评论中,type 没有错误地打印出来,但不是我想要的方式。
    • @Qiau 我不知道我需要序列化。
    • @tijko:是的,但是在您的代码示例中,您尝试使用 type(data) 作为参数调用 write(),除非您之前将其转换为字符串,否则这将不起作用。
    • @tijko: 常见的事情 :) 查看模块“Pickle”
    猜你喜欢
    • 2011-05-30
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2012-06-04
    • 2013-05-17
    相关资源
    最近更新 更多