Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 使用twisted python侦听到服务器的连接时获取用户输入_Python 2.7_Twisted - Fatal编程技术网

Python 2.7 使用twisted python侦听到服务器的连接时获取用户输入

Python 2.7 使用twisted python侦听到服务器的连接时获取用户输入,python-2.7,twisted,Python 2.7,Twisted,我目前正在尝试使用TwistedPython处理一个项目,我的问题是,我试图在获取用户输入的同时使用listenTCP()侦听连接。我最初查找了这个问题,发现stdio.StandardIO似乎是最有效的方法,因为我已经在使用Twisted。我还看到了twisted matrix上的代码示例,但是,鉴于在执行tcp任务时需要从套接字读取数据并收集用户输入,我正在努力解决如何将示例代码应用于我的特定问题 我正在进行的项目要大得多,但是小示例代码说明了我正在尝试做什么,并隔离了我遇到的问题。我非常感

我目前正在尝试使用TwistedPython处理一个项目,我的问题是,我试图在获取用户输入的同时使用listenTCP()侦听连接。我最初查找了这个问题,发现stdio.StandardIO似乎是最有效的方法,因为我已经在使用Twisted。我还看到了twisted matrix上的代码示例,但是,鉴于在执行tcp任务时需要从套接字读取数据并收集用户输入,我正在努力解决如何将示例代码应用于我的特定问题

我正在进行的项目要大得多,但是小示例代码说明了我正在尝试做什么,并隔离了我遇到的问题。我非常感谢任何帮助我解决问题的人

Server.py

from twisted.internet.protocol import Factory
from twisted.protocols import basic
from twisted.internet import reactor, protocol, stdio
from Tkinter import *
import os, sys

class ServerProtocol(protocol.Protocol):
    def __init__(self, factory):
        self.factory = factory
        stdio.StandardIO(self)

    def connectionMade(self):
        self.factory.numConnections += 1
        self.factory.clients.append(self)

    def dataReceived(self, data):
        try:
            print 'receiving data'
            print data
        except Exception, e:
            print e

    def connectionLost(self, reason):
        self.factory.numConnections -= 1
        self.factory.clients.remove(self)

class ServerFactory(Factory):
    numConnections = 0
    def buildProtocol(self, addr):
        return ServerProtocol(self)

class StdioCommandLine(basic.LineReceiver):
    from os import linesep as delimiter
    def connectionMade(self):
        self.transport.write('>>> ')
    def lineReceived(self, line):
        self.sendLine('Echo: ' + line)
        self.transport.write('>>> ')

reactor.listenTCP(9001, ServerFactory())
stdio.StandardIO(StdioCommandLine())
reactor.run()
Client.py

from twisted.internet import reactor, protocol
import os, time, sys
import argparse

class MessageClientProtocol(protocol.Protocol):

    def __init__(self, factory):
        self.factory = factory

    def connectionMade(self):
        self.sendMessage()

    def sendMessage(self):
        print 'sending message'
        try:
            self.transport.write('hello world')
        except e as Exception:
            print e

    def dataReceived(self, data):
        print 'received: ', data
        self.sendMessage()

class MessageClientFactory(protocol.ClientFactory):

    def __init__(self, message):
        self.message = message

    def buildProtocol(self, addr):
        return MessageClientProtocol(self)

    def clientConnectionFailed(self, connector, reason):
        print 'Connection Failed: ', reason.getErrorMessage()
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print 'Connection Lost: ', reason.getErrorMessage()

reactor.connectTCP('192.168.1.70', 9001, MessageClientFactory('hello world - client'))
reactor.run()
目前,上述代码返回一个未经处理的错误,如下所示。这演示了如何使用stdin,然后将数据返回到stdout和导致错误的客户端:

python服务器.py

>>>你好

回声:你好

>>>未处理的错误回溯(最近一次呼叫上次):

文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/twisted/Python/log.py”, 第84行,在callWithContext中

返回context.call({ILogContext:newCtx},func,*args,**kw)

文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/twisted/Python/context.py”, 第118行,callWithContext返回 self.currentContext().callWithContext(ctx,func,*args,**kw)

文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/twisted/Python/context.py”, 第81行,在callWithContext中

返回函数(*参数,**kw)

文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/twisted/internet/selectreactor.py”, 第149行,输入或写入

为什么=getattr(可选,方法)()

---这里有个例外---

文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/twisted/internet/tcp.py”, 第1067行,在doRead中
协议=s


你提供的追踪似乎被切断了。我试图在我的机器上运行代码,它显示了以下回溯:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
--- <exception caught here> ---
  File "/usr/lib/python2.7/site-packages/twisted/internet/tcp.py", line 1067, in doRead
    protocol = self.factory.buildProtocol(self._buildAddr(addr))
  File "Server.py", line 30, in buildProtocol
    return ServerProtocol(self)
  File "Server.py", line 10, in __init__
    stdio.StandardIO(self)
  File "/usr/lib/python2.7/site-packages/twisted/internet/_posixstdio.py", line 42, in __init__
    self.protocol.makeConnection(self)
  File "/usr/lib/python2.7/site-packages/twisted/internet/protocol.py", line 490, in makeConnection
    self.connectionMade()
  File "Server.py", line 14, in connectionMade
    self.factory.clients.append(self)
exceptions.AttributeError: ServerFactory instance has no attribute 'clients'

你提供的追踪似乎被切断了。我试图在我的机器上运行代码,它显示了以下回溯:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
--- <exception caught here> ---
  File "/usr/lib/python2.7/site-packages/twisted/internet/tcp.py", line 1067, in doRead
    protocol = self.factory.buildProtocol(self._buildAddr(addr))
  File "Server.py", line 30, in buildProtocol
    return ServerProtocol(self)
  File "Server.py", line 10, in __init__
    stdio.StandardIO(self)
  File "/usr/lib/python2.7/site-packages/twisted/internet/_posixstdio.py", line 42, in __init__
    self.protocol.makeConnection(self)
  File "/usr/lib/python2.7/site-packages/twisted/internet/protocol.py", line 490, in makeConnection
    self.connectionMade()
  File "Server.py", line 14, in connectionMade
    self.factory.clients.append(self)
exceptions.AttributeError: ServerFactory instance has no attribute 'clients'

您的回溯结束似乎已被切断。抱歉,最后一行是protocol=s,但这是显示的全部错误。另外,在客户端连接到服务器后,上面的错误会显示出来,然后我可以继续提供用户输入。任何进一步的客户端连接也会出现同样的错误。这很奇怪。查看tcp.py中的代码,第1067行,该行应该是
protocol=self.factory.buildProtocol(self.\u buildAddr(addr))
。我也在读相同的代码,即:
protocol=self.factory.buildProtocol(self.\u buildAddr(addr))
。我已经仔细检查了错误信息,在我纠正之前的最后几行中,格式已经混乱。我试图在本地重现问题,在我的机器上,它显示了完整的回溯。我将写一个包含回溯的答案。回溯的结尾似乎被切断。抱歉
protocol=s
是最后一行,但这是显示的全部错误。另外,在客户端连接到服务器后,上面的错误会显示出来,然后我可以继续提供用户输入。任何进一步的客户端连接也会出现同样的错误。这很奇怪。查看tcp.py中的代码,第1067行,该行应该是
protocol=self.factory.buildProtocol(self.\u buildAddr(addr))
。我也在读相同的代码,即:
protocol=self.factory.buildProtocol(self.\u buildAddr(addr))
。我已经仔细检查了错误信息,在我纠正之前的最后几行中,格式已经混乱。我试图在本地重现问题,在我的机器上,它显示了完整的回溯。我会写一个答案,包括回溯。虽然上面没有完全纠正问题,但它让我想到了被抑制的输出。我在Ubuntu上启动了一个VM,运行了相同的代码,出现了完整的错误消息,现在我已经实现了上面的答案,并重写了代码,因为我现在有了完整的图片。我觉得奇怪的是,在OSX‘el capitan’10.11.4和twisted的最新版本上,我没有得到相同的错误,但是我感谢您帮助我对我的问题得出结论。虽然上面没有完全纠正问题,但它让我想到了被抑制的输出。我在Ubuntu上启动了一个VM,运行了相同的代码,出现了完整的错误消息,现在我已经实现了上面的答案,并重写了代码,因为我现在有了完整的图片。我觉得奇怪的是,在OSX'el capitan'10.11.4的最新版本twisted上,我没有收到相同的错误,但是我感谢您帮助我对我的问题得出结论。