Simple server/client application

M

MBW

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'
 
S

Simon Percivall

You're calling the grid() method on the Entry object you're
instanciating. Are you sure that the grid() method returns the Entry
object so that you're actually binding it to self.myAddress?
 
J

jmdeschamps

Simon said:
You're calling the grid() method on the Entry object you're
instanciating. Are you sure that the grid() method returns the Entry
object so that you're actually binding it to self.myAddress?

The widget maker should do it in two steps instead of one.
So instead of:

self.myAddress=Entry(self.root,relief=SUNKEN).grid(row=1,column=1,sticky=W)
Make it thus:
self.myAddress = Entry(self.root,relief=SUNKEN) # make it and
keep it here
self.address.grid(row=1,column=1,sticky=W) # grid it here
in the create_widgets method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top