Simple server/client application

MBW matt.walsh at gmail.com
Mon Oct 24 22:11:06 CEST 2005


The following code is for a simple server/client asplication that
allows the user to toggle between serve and or client modes and
send/recieve a message however i am getting an Attribute error on my
entry widgets when I try and call get() to get the port and ip address
from those fields, I'm sure it's something simple I haven't grasped...

#START

from Tkinter import *
from socket import *
import sys

class mySCS:
    def __init__(self,root):
        self.root = root
        self.root.title("My Simple Client/Server           ")
        self.mode = "Server"
        self.create_widgets()

        self.root.mainloop()
        return None

    def create_widgets(self):
        self.portLbl = Label(self.root,text="Port
number:").grid(sticky=E)
        self.AddrLbl = Label(self.root,text="IP
Address:").grid(row=1,sticky=E)
        self.myPort =
Entry(self.root,relief=SUNKEN).grid(column=1,row=0,sticky=W)
        self.myAddress =
Entry(self.root,relief=SUNKEN).grid(row=1,column=1,sticky=W)
        self.SCSToggle =
Radiobutton(self.root,text="Server",value="Server",variable=self.mode).grid(row=2,column=0,sticky=W)
        self.SCSToggle =
Radiobutton(self.root,text="Client",value="Client",variable=self.mode).grid(row=2,column=1,sticky=W)
        self.messageLbl = Label(self.root,text="Message:").grid(row=4)
        self.myMessage = Text(self.root,height=6).grid(row=4,column=1)
        self.statusLbl = Label(self.root,text="Status:").grid(row=5)
        self.myStatus = Text(self.root,height=6).grid(row=5,column=1)
        self.myConnect =
Button(self.root,text='Run',command=self.connect).grid(row=6,
columnspan=2)
        return None

    def connect(self):
        Host = self.myAddress.get()
        Port = self.myPort.get()

        if self.mode == "Server":

            sockobj = socket(AF_INET, SOCK_STREAM)
            sockobj.bind((Host,Port))
            sockobj.listen(5)
            myStatus.insert("Waiting on Conncetion...\n")
            while 1:
                connection, address = sockobj.accept()
                myStatus.insert('Server connection by', address)
                while 1:
                    data = connection.recv(1024)
                    if not data: break
                    self.myStatus.insert('Data Recievced ' +str(data))
                    connection.send('Echo=>' + data)
                connection.close()

        if self.mode == "Client":

            message = myMessage.get()
            if len(sys.argv) > 1:
                Host =sys.argv[1]
            if len(sys.argv) > 2:
                message = sys.argv[2:]

            sockobj = socket(AF_INET, SOCK_STREAM)
            sockobj.connect((Host, Port))

            sockobj.send(message)
            data = sockobj.recv(1024)
            self.myStatus.insert('Client recived:', str(data))

            sockobj.close()
        return None

if __name__ == "__main__":
    root = Tk()
    myobj = mySCS(root)

#END

Error message:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "C:\Documents and Settings\Matt\My
Documents\work\eclipse\workspace\PSI\View\mySCS.py", line 30, in
connect
    Host = self.myAddress.get()
AttributeError: 'NoneType' object has no attribute 'get'




More information about the Python-list mailing list