无法使用Twisted写入文件

2024-09-29 23:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个代码片段,我正在使用它与客户机建立连接,然后将他的所有文本写入一个文件。这是代码

from twisted.internet.protocol import Protocol, Factory
    from twisted.internet import reactor
    import ctypes  # An included library with Python install.
    class DataTransfer(Protocol):
        def connectionMade(self):
                #self.transport.write("""connected""")
                fwrite = open('/Applications/mamp/htdocs/datatest.txt','r+')
                self.factory.clients.append(self)
                print "clients are ", self.factory.clients
                self.username = ""
                self.password = ""
                self.auth = False
                self.ipaddress = self.transport.getPeer()
                print self.ipaddress

        def connectionLost(self, reason):
            fwrite.close()
            self.factory.clients.remove(self)
            print reason

        def dataReceived(self, data):
            print data
            fwrite.write(data)
            a = data.split(':')
            if len(a) > 1:
                        command = a[0]
                        content = a[1]

                        msg = ""

                        self.message(msg)

        def message(self, message):
                self.transport.write(message + '\n')

    factory = Factory()
    factory.protocol = DataTransfer
    factory.clients = []

    reactor.listenTCP(8889, factory)
    print "Server started"
    reactor.run()

我正在执行这个命令pythonw socketListner.py。服务器启动成功。但是当我使用netcat命令连接到它时,我在终端中得到以下信息,连接就关闭了。你知道吗

Unhandled Error
    Traceback (most recent call last):
      File "socketListner.py", line 42, in <module>
        reactor.run()
      File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1194, in run
        self.mainLoop()
      File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
        self.doIteration(t)
      File "/Library/Python/2.7/site-packages/twisted/internet/selectreactor.py", line 143, in doSelect
        _logrun(selectable, _drdw, selectable, method)
    --- <exception caught here> ---
      File "/Library/Python/2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
        return callWithContext({"system": lp}, func, *args, **kw)
      File "/Library/Python/2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
        return context.call({ILogContext: newCtx}, func, *args, **kw)
      File "/Library/Python/2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
        return self.currentContext().callWithContext(ctx, func, *args, **kw)
      File "/Library/Python/2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
        return func(*args,**kw)
      File "/Library/Python/2.7/site-packages/twisted/internet/selectreactor.py", line 154, in _doReadOrWrite
        self._disconnectSelectable(selectable, why, method=="doRead")
      File "/Library/Python/2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
        selectable.connectionLost(failure.Failure(why))
      File "/Library/Python/2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
        protocol.connectionLost(reason)
      File "socketListner.py", line 17, in connectionLost
        fwrite.close()
    exceptions.NameError: global name 'fwrite' is not defined

Tags: inpyselffactorypackageslinelibrarytwisted

热门问题