P
PeterG
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.internet import reactor
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
factory = Factory()
#this is a list
factory.transports = []
#this is a dictionary
userNicknames = {}
class SimpleLogger(LineReceiver):
def connectionMade(self):
self.factory.transports.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(userNicknames.get(self.transport.client)) + '
Disconnected' + '\r\n'
print who
userNicknames[self.transport.client] = ''
for transport in self.factory.transports:
transport.write(who)
def lineReceived(self, 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.get(self.transport.client) == '':
#if the username is already in the dictionary someone is
#already using it so ask for another one.
if line in userNicknames.values():
self.transport.write('That nickname is already in use,
please use another:')
else:
userNicknames[self.transport.client] = line
#print userNicknames.items()
message = userNicknames.get(self.transport.client) + '
has joined\r\n'
for transport in self.factory.transports:
transport.write(message)
else:
message = userNicknames.get(self.transport.client) + ': ' +
line + '\r\n'
for transport in self.factory.transports:
transport.write(message)
factory.protocol = SimpleLogger
reactor.listenTCP(1234, factory)
reactor.run()
client.py
----------
from wxPython.wx import *
import wx
from twisted.internet import wxreactor
wxreactor.install()
from twisted.internet import reactor
from twisted.internet.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(bkg, -1, 'IP Address: ')
ipAdd = wx.TextCtrl(bkg)
ipAdd.SetToolTipString('Please enter the server IP address
here.')
spacer1 = wx.StaticText(bkg, -1, ' ')
portNoLab = wx.StaticText(bkg, -1, 'Port No: ')
portNo = wx.TextCtrl(bkg)
portNo.SetToolTipString('Please enter the port number the
server is using here.')
spacer2 = wx.StaticText(bkg, -1, ' ')
connectButton = wx.Button(bkg, label='Connect')
connectButton.SetToolTipString('Click this button to connect to
the server.')
connectButton.Bind(wx.EVT_BUTTON, self.connectMe)
disconnectButton = wx.Button(bkg, label='Disconnect')
disconnectButton.SetToolTipString('Click this button to
disconnect from the server.')
disconnectButton.Bind(wx.EVT_BUTTON, self.disconnectMe)
messages = wx.TextCtrl(bkg, style=(wx.TE_MULTILINE |
wx.HSCROLL))
newMsg = wx.TextCtrl(bkg)
sendButton = wx.Button(bkg, label='Send')
sendButton.SetToolTipString('Click this button to send a
message to the server.')
sendButton.Bind(wx.EVT_BUTTON, self.sendMe)
hbox1 = wx.BoxSizer()
hbox1.Add(ipAddLab, proportion=0, flag=wx.EXPAND)
hbox1.Add(ipAdd, proportion=0, flag=wx.EXPAND)
hbox1.Add(spacer1, proportion=0, flag=wx.EXPAND)
hbox1.Add(portNoLab, proportion=0, flag=wx.EXPAND)
hbox1.Add(portNo, proportion=0, flag=wx.EXPAND)
hbox1.Add(spacer2, proportion=0, flag=wx.EXPAND)
hbox1.Add(connectButton, proportion=0, flag=wx.LEFT, border=5)
hbox1.Add(disconnectButton, proportion=0, flag=wx.LEFT,
border=5)
hbox2 = wx.BoxSizer()
hbox2.Add(newMsg, proportion=1, flag=wx.EXPAND | wx.LEFT |
wx.LEFT | wx.LEFT, border=5)
hbox2.Add(sendButton, 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(messages, 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(vbox)
ipAdd.WriteText('localhost')
portNo.WriteText('1234')
frame.Show(true)
return true
def sendMe(self, e):
msg = newMsg.GetValue() + '\n'
messages.WriteText(msg)
newMsg.SetValue('')
def disconnectMe(self, e):
messages.WriteText('Disconnecting from server...\n')
self.transport.loseConnection()
def connectMe(self, e):
messages.WriteText('Connecting to server...\n')
c = ClientCreator(reactor, imApp)
ip = str(ipAdd.GetValue())
port = int(portNo.GetValue())
c.connectTCP(ip, port)
def mainProg():
app = imApp(0)
app.buildMe()
reactor.registerWxApp(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.internet import reactor
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
factory = Factory()
#this is a list
factory.transports = []
#this is a dictionary
userNicknames = {}
class SimpleLogger(LineReceiver):
def connectionMade(self):
self.factory.transports.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(userNicknames.get(self.transport.client)) + '
Disconnected' + '\r\n'
print who
userNicknames[self.transport.client] = ''
for transport in self.factory.transports:
transport.write(who)
def lineReceived(self, 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.get(self.transport.client) == '':
#if the username is already in the dictionary someone is
#already using it so ask for another one.
if line in userNicknames.values():
self.transport.write('That nickname is already in use,
please use another:')
else:
userNicknames[self.transport.client] = line
#print userNicknames.items()
message = userNicknames.get(self.transport.client) + '
has joined\r\n'
for transport in self.factory.transports:
transport.write(message)
else:
message = userNicknames.get(self.transport.client) + ': ' +
line + '\r\n'
for transport in self.factory.transports:
transport.write(message)
factory.protocol = SimpleLogger
reactor.listenTCP(1234, factory)
reactor.run()
client.py
----------
from wxPython.wx import *
import wx
from twisted.internet import wxreactor
wxreactor.install()
from twisted.internet import reactor
from twisted.internet.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(bkg, -1, 'IP Address: ')
ipAdd = wx.TextCtrl(bkg)
ipAdd.SetToolTipString('Please enter the server IP address
here.')
spacer1 = wx.StaticText(bkg, -1, ' ')
portNoLab = wx.StaticText(bkg, -1, 'Port No: ')
portNo = wx.TextCtrl(bkg)
portNo.SetToolTipString('Please enter the port number the
server is using here.')
spacer2 = wx.StaticText(bkg, -1, ' ')
connectButton = wx.Button(bkg, label='Connect')
connectButton.SetToolTipString('Click this button to connect to
the server.')
connectButton.Bind(wx.EVT_BUTTON, self.connectMe)
disconnectButton = wx.Button(bkg, label='Disconnect')
disconnectButton.SetToolTipString('Click this button to
disconnect from the server.')
disconnectButton.Bind(wx.EVT_BUTTON, self.disconnectMe)
messages = wx.TextCtrl(bkg, style=(wx.TE_MULTILINE |
wx.HSCROLL))
newMsg = wx.TextCtrl(bkg)
sendButton = wx.Button(bkg, label='Send')
sendButton.SetToolTipString('Click this button to send a
message to the server.')
sendButton.Bind(wx.EVT_BUTTON, self.sendMe)
hbox1 = wx.BoxSizer()
hbox1.Add(ipAddLab, proportion=0, flag=wx.EXPAND)
hbox1.Add(ipAdd, proportion=0, flag=wx.EXPAND)
hbox1.Add(spacer1, proportion=0, flag=wx.EXPAND)
hbox1.Add(portNoLab, proportion=0, flag=wx.EXPAND)
hbox1.Add(portNo, proportion=0, flag=wx.EXPAND)
hbox1.Add(spacer2, proportion=0, flag=wx.EXPAND)
hbox1.Add(connectButton, proportion=0, flag=wx.LEFT, border=5)
hbox1.Add(disconnectButton, proportion=0, flag=wx.LEFT,
border=5)
hbox2 = wx.BoxSizer()
hbox2.Add(newMsg, proportion=1, flag=wx.EXPAND | wx.LEFT |
wx.LEFT | wx.LEFT, border=5)
hbox2.Add(sendButton, 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(messages, 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(vbox)
ipAdd.WriteText('localhost')
portNo.WriteText('1234')
frame.Show(true)
return true
def sendMe(self, e):
msg = newMsg.GetValue() + '\n'
messages.WriteText(msg)
newMsg.SetValue('')
def disconnectMe(self, e):
messages.WriteText('Disconnecting from server...\n')
self.transport.loseConnection()
def connectMe(self, e):
messages.WriteText('Connecting to server...\n')
c = ClientCreator(reactor, imApp)
ip = str(ipAdd.GetValue())
port = int(portNo.GetValue())
c.connectTCP(ip, port)
def mainProg():
app = imApp(0)
app.buildMe()
reactor.registerWxApp(app)
reactor.run()
if __name__ == '__main__':
mainProg()