Kullanarak dosyaya yazılamıyor Bir istemci ile bağlantı kurmak için kullandığım bu kod parçacığım var ve tüm metni bir dosyaya yazıyor. İşte kod
Twisted
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()
ben bu komutu pythonw socketListner.py
yürütme am olduğunu. Sunucu başarıyla başlatılıyor. Ama netcat komutunu kullanarak bağlandığımda, terminalimde aşağıdakileri alıyorum ve bağlantı kapanıyor. fwrite
değişken connectionMade
yerel olarak tanımlanır Sizin
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