473,388 Members | 1,385 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Twisted/wxPython Problem...

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()

Apr 24 '06 #1
1 2769
PeterG wrote:
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'


It seems that your client inherits from two classes, but doesn't invoke
their respective constructors. That makes python only call the first
classes constructor, as this simple experiment shows:

class A(object):
def __init__(self):
print "I'm A"

class B(object):
def __init__(self):
print "I'm B"
class C(A,B):
pass

C()
-> I'm A
so - create a constructor, invoke both constructors of your super-classes
either explicitly or using super (make sure you understand super!)
Regards,

Diez
Apr 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Moore | last post by:
I hit a problem yesterday with my mail connection. In a desparate attempt to understand what was going on, I wanted to log the connection traffic. After a bit of searching, I found a post on c.l.p...
11
by: mir nazim | last post by:
hi, i m planning to start writing intranet applications and want ur real cool advices for choosing the correct platform. the choice is between the three: 1. Twisted 2. Medusa 3. Zope (i do...
1
by: marchew | last post by:
hi, i have a problem integrating wxPython and Twisted under Win32. my application consumes 40-50% of CPU resources when wxFrame is visible and reactor (twisted) is running. i looked at...
2
by: Taki Jeden | last post by:
Hi Anybody used wxPython with twisted? I started putting together a Twisted-based app with wx GUI, and the widgets just don't work - some controls do not show up etc. - at least on my system....
3
by: flupke | last post by:
Hi, previously i made an application that used sockets to do some network based processing. My next app is again going to be a client/server app and i wanted to see if i can't find an easier...
2
by: Daniel Bickett | last post by:
Hello, I am writing an application using two event-driven libraries: wxPython, and twisted. The first problem I encountered in the program is the confliction between the two all-consuming...
2
by: SeSe | last post by:
Hi, I am new to Twisted. I use a Twisted 1.3.0 on MS Windows XP Home Edition, my python version is 2.3 I try the TCP echoserv.py and echoclient.py example. But the client always fail with...
2
by: Christopher Subich | last post by:
I'm building an application that makes several user-specified internet connections; twisted meets my needs more or less perfectly. I'm running into a problem, however, in that twisted is not...
0
by: qvx | last post by:
I want to start twisted app from another GUI application and not via twistd. It works fine when started via twistd (1 & 2) but not when I try to start it manually (1 & 3) - nothing is listening...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.