471,071 Members | 911 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Simple server/client application

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'

Oct 24 '05 #1
3 2330
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?

Oct 24 '05 #2

Simon Percivall wrote:
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.

Oct 25 '05 #3
MBW
thanks both of you,

I should have realized what was gonig on

Cheers,
Matt

Oct 25 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by GeekBoy | last post: by
18 posts views Thread by Q. John Chen | last post: by
10 posts views Thread by serge calderara | last post: by
1 post views Thread by Josema | last post: by
2 posts views Thread by Vitali Gontsharuk | last post: by
10 posts views Thread by Phillip Taylor | last post: by
reply views Thread by leo001 | last post: by

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.