Hi,
I am relatively new to Python, and am learning it as part of a
university
module...
Im currently undertaking a project to create an IM server and IM gui
client.
I have a very basic server, and a basic gui client. I can get my
client to
connect to my server, but cant get it to disconnect or send messages to
the
server.
I am getting the following error when I click on the 'Disconnect'
button -
AttributeError: 'NoneType' object has no attribute 'loseConnection '
I have attached the code for both the server and the client below this.
I am using the Twisted and wxPython packages, and as previously stated
am
fairly new to Python so would appreciate any help anyone can offer.
Thanks,
Peter
server.py
------------
from twisted.interne t import reactor
from twisted.interne t.protocol import Factory
from twisted.protoco ls.basic import LineReceiver
factory = Factory()
#this is a list
factory.transpo rts = []
#this is a dictionary
userNicknames = {}
class SimpleLogger(Li neReceiver):
def connectionMade( self):
self.factory.tr ansports.append (self.transport )
userNicknames[self.transport. client] = ''
#write to the client
self.transport. write("Welcome to Chris & Pete's chat server!\r\n")
self.transport. write("Please enter your nickname:\r\n")
#prints on the server screen
print 'got connection from', self.transport. client
def connectionLost( self, reason):
who = str(userNicknam es.get(self.tra nsport.client)) + '
Disconnected' + '\r\n'
print who
userNicknames[self.transport. client] = ''
for transport in self.factory.tr ansports:
transport.write (who)
def lineReceived(se lf, line):
#if the users nickname in the dictionary (userNicknames) is
blank, create a
#value in the dictionary with the line just received.
#if the user already has a nickname then it must be a message
they are writing.
#So instead print out the message
if userNicknames.g et(self.transpo rt.client) == '':
#if the username is already in the dictionary someone is
#already using it so ask for another one.
if line in userNicknames.v alues():
self.transport. write('That nickname is already in use,
please use another:')
else:
userNicknames[self.transport. client] = line
#print userNicknames.i tems()
message = userNicknames.g et(self.transpo rt.client) + '
has joined\r\n'
for transport in self.factory.tr ansports:
transport.write (message)
else:
message = userNicknames.g et(self.transpo rt.client) + ': ' +
line + '\r\n'
for transport in self.factory.tr ansports:
transport.write (message)
factory.protoco l = SimpleLogger
reactor.listenT CP(1234, factory)
reactor.run()
client.py
----------
from wxPython.wx import *
import wx
from twisted.interne t import wxreactor
wxreactor.insta ll()
from twisted.interne t import reactor
from twisted.interne t.protocol import Protocol, ClientCreator
class imApp(wxApp, Protocol):
def buildMe(self):
frame = wx.Frame(None, title="IM Client", size=(800, 550))
bkg = wx.Panel(frame)
global ipAdd
global portNo
global messages
global newMsg
ipAddLab = wx.StaticText(b kg, -1, 'IP Address: ')
ipAdd = wx.TextCtrl(bkg )
ipAdd.SetToolTi pString('Please enter the server IP address
here.')
spacer1 = wx.StaticText(b kg, -1, ' ')
portNoLab = wx.StaticText(b kg, -1, 'Port No: ')
portNo = wx.TextCtrl(bkg )
portNo.SetToolT ipString('Pleas e enter the port number the
server is using here.')
spacer2 = wx.StaticText(b kg, -1, ' ')
connectButton = wx.Button(bkg, label='Connect' )
connectButton.S etToolTipString ('Click this button to connect to
the server.')
connectButton.B ind(wx.EVT_BUTT ON, self.connectMe)
disconnectButto n = wx.Button(bkg, label='Disconne ct')
disconnectButto n.SetToolTipStr ing('Click this button to
disconnect from the server.')
disconnectButto n.Bind(wx.EVT_B UTTON, self.disconnect Me)
messages = wx.TextCtrl(bkg , style=(wx.TE_MU LTILINE |
wx.HSCROLL))
newMsg = wx.TextCtrl(bkg )
sendButton = wx.Button(bkg, label='Send')
sendButton.SetT oolTipString('C lick this button to send a
message to the server.')
sendButton.Bind (wx.EVT_BUTTON, self.sendMe)
hbox1 = wx.BoxSizer()
hbox1.Add(ipAdd Lab, proportion=0, flag=wx.EXPAND)
hbox1.Add(ipAdd , proportion=0, flag=wx.EXPAND)
hbox1.Add(space r1, proportion=0, flag=wx.EXPAND)
hbox1.Add(portN oLab, proportion=0, flag=wx.EXPAND)
hbox1.Add(portN o, proportion=0, flag=wx.EXPAND)
hbox1.Add(space r2, proportion=0, flag=wx.EXPAND)
hbox1.Add(conne ctButton, proportion=0, flag=wx.LEFT, border=5)
hbox1.Add(disco nnectButton, proportion=0, flag=wx.LEFT,
border=5)
hbox2 = wx.BoxSizer()
hbox2.Add(newMs g, proportion=1, flag=wx.EXPAND | wx.LEFT |
wx.LEFT | wx.LEFT, border=5)
hbox2.Add(sendB utton, proportion=0, flag=wx.LEFT, border=5)
vbox = wx.BoxSizer(wx. VERTICAL)
vbox.Add(hbox1, proportion=0, flag=wx.EXPAND | wx.ALL,
border=5)
vbox.Add(messag es, proportion=1, flag=wx.EXPAND | wx.LEFT |
wx.LEFT | wx.LEFT, border=5)
vbox.Add(hbox2, proportion=1, flag=wx.EXPAND | wx.ALL,
border=5)
bkg.SetSizer(vb ox)
ipAdd.WriteText ('localhost')
portNo.WriteTex t('1234')
frame.Show(true )
return true
def sendMe(self, e):
msg = newMsg.GetValue () + '\n'
messages.WriteT ext(msg)
newMsg.SetValue ('')
def disconnectMe(se lf, e):
messages.WriteT ext('Disconnect ing from server...\n')
self.transport. loseConnection( )
def connectMe(self, e):
messages.WriteT ext('Connecting to server...\n')
c = ClientCreator(r eactor, imApp)
ip = str(ipAdd.GetVa lue())
port = int(portNo.GetV alue())
c.connectTCP(ip , port)
def mainProg():
app = imApp(0)
app.buildMe()
reactor.registe rWxApp(app)
reactor.run()
if __name__ == '__main__':
mainProg()
I am relatively new to Python, and am learning it as part of a
university
module...
Im currently undertaking a project to create an IM server and IM gui
client.
I have a very basic server, and a basic gui client. I can get my
client to
connect to my server, but cant get it to disconnect or send messages to
the
server.
I am getting the following error when I click on the 'Disconnect'
button -
AttributeError: 'NoneType' object has no attribute 'loseConnection '
I have attached the code for both the server and the client below this.
I am using the Twisted and wxPython packages, and as previously stated
am
fairly new to Python so would appreciate any help anyone can offer.
Thanks,
Peter
server.py
------------
from twisted.interne t import reactor
from twisted.interne t.protocol import Factory
from twisted.protoco ls.basic import LineReceiver
factory = Factory()
#this is a list
factory.transpo rts = []
#this is a dictionary
userNicknames = {}
class SimpleLogger(Li neReceiver):
def connectionMade( self):
self.factory.tr ansports.append (self.transport )
userNicknames[self.transport. client] = ''
#write to the client
self.transport. write("Welcome to Chris & Pete's chat server!\r\n")
self.transport. write("Please enter your nickname:\r\n")
#prints on the server screen
print 'got connection from', self.transport. client
def connectionLost( self, reason):
who = str(userNicknam es.get(self.tra nsport.client)) + '
Disconnected' + '\r\n'
print who
userNicknames[self.transport. client] = ''
for transport in self.factory.tr ansports:
transport.write (who)
def lineReceived(se lf, line):
#if the users nickname in the dictionary (userNicknames) is
blank, create a
#value in the dictionary with the line just received.
#if the user already has a nickname then it must be a message
they are writing.
#So instead print out the message
if userNicknames.g et(self.transpo rt.client) == '':
#if the username is already in the dictionary someone is
#already using it so ask for another one.
if line in userNicknames.v alues():
self.transport. write('That nickname is already in use,
please use another:')
else:
userNicknames[self.transport. client] = line
#print userNicknames.i tems()
message = userNicknames.g et(self.transpo rt.client) + '
has joined\r\n'
for transport in self.factory.tr ansports:
transport.write (message)
else:
message = userNicknames.g et(self.transpo rt.client) + ': ' +
line + '\r\n'
for transport in self.factory.tr ansports:
transport.write (message)
factory.protoco l = SimpleLogger
reactor.listenT CP(1234, factory)
reactor.run()
client.py
----------
from wxPython.wx import *
import wx
from twisted.interne t import wxreactor
wxreactor.insta ll()
from twisted.interne t import reactor
from twisted.interne t.protocol import Protocol, ClientCreator
class imApp(wxApp, Protocol):
def buildMe(self):
frame = wx.Frame(None, title="IM Client", size=(800, 550))
bkg = wx.Panel(frame)
global ipAdd
global portNo
global messages
global newMsg
ipAddLab = wx.StaticText(b kg, -1, 'IP Address: ')
ipAdd = wx.TextCtrl(bkg )
ipAdd.SetToolTi pString('Please enter the server IP address
here.')
spacer1 = wx.StaticText(b kg, -1, ' ')
portNoLab = wx.StaticText(b kg, -1, 'Port No: ')
portNo = wx.TextCtrl(bkg )
portNo.SetToolT ipString('Pleas e enter the port number the
server is using here.')
spacer2 = wx.StaticText(b kg, -1, ' ')
connectButton = wx.Button(bkg, label='Connect' )
connectButton.S etToolTipString ('Click this button to connect to
the server.')
connectButton.B ind(wx.EVT_BUTT ON, self.connectMe)
disconnectButto n = wx.Button(bkg, label='Disconne ct')
disconnectButto n.SetToolTipStr ing('Click this button to
disconnect from the server.')
disconnectButto n.Bind(wx.EVT_B UTTON, self.disconnect Me)
messages = wx.TextCtrl(bkg , style=(wx.TE_MU LTILINE |
wx.HSCROLL))
newMsg = wx.TextCtrl(bkg )
sendButton = wx.Button(bkg, label='Send')
sendButton.SetT oolTipString('C lick this button to send a
message to the server.')
sendButton.Bind (wx.EVT_BUTTON, self.sendMe)
hbox1 = wx.BoxSizer()
hbox1.Add(ipAdd Lab, proportion=0, flag=wx.EXPAND)
hbox1.Add(ipAdd , proportion=0, flag=wx.EXPAND)
hbox1.Add(space r1, proportion=0, flag=wx.EXPAND)
hbox1.Add(portN oLab, proportion=0, flag=wx.EXPAND)
hbox1.Add(portN o, proportion=0, flag=wx.EXPAND)
hbox1.Add(space r2, proportion=0, flag=wx.EXPAND)
hbox1.Add(conne ctButton, proportion=0, flag=wx.LEFT, border=5)
hbox1.Add(disco nnectButton, proportion=0, flag=wx.LEFT,
border=5)
hbox2 = wx.BoxSizer()
hbox2.Add(newMs g, proportion=1, flag=wx.EXPAND | wx.LEFT |
wx.LEFT | wx.LEFT, border=5)
hbox2.Add(sendB utton, proportion=0, flag=wx.LEFT, border=5)
vbox = wx.BoxSizer(wx. VERTICAL)
vbox.Add(hbox1, proportion=0, flag=wx.EXPAND | wx.ALL,
border=5)
vbox.Add(messag es, proportion=1, flag=wx.EXPAND | wx.LEFT |
wx.LEFT | wx.LEFT, border=5)
vbox.Add(hbox2, proportion=1, flag=wx.EXPAND | wx.ALL,
border=5)
bkg.SetSizer(vb ox)
ipAdd.WriteText ('localhost')
portNo.WriteTex t('1234')
frame.Show(true )
return true
def sendMe(self, e):
msg = newMsg.GetValue () + '\n'
messages.WriteT ext(msg)
newMsg.SetValue ('')
def disconnectMe(se lf, e):
messages.WriteT ext('Disconnect ing from server...\n')
self.transport. loseConnection( )
def connectMe(self, e):
messages.WriteT ext('Connecting to server...\n')
c = ClientCreator(r eactor, imApp)
ip = str(ipAdd.GetVa lue())
port = int(portNo.GetV alue())
c.connectTCP(ip , port)
def mainProg():
app = imApp(0)
app.buildMe()
reactor.registe rWxApp(app)
reactor.run()
if __name__ == '__main__':
mainProg()
Comment