python - How to get input from SocketServer MyTCPHandler to another class -
i trying input mytcphandler class network in following program. ideally like, example, take self.data mytcphandler , change self._title in network. usual, apologize simple question, new classes in python , struggling bit one. help!
from tkinter import * import time import math import lockfile import functions import socketserver import threading class network(frame): """ implements stop watch frame widget. """ def __init__(self, parent=none, **kw): frame.__init__(self, parent, kw) host, port = "192.168.2.3", 9999 self._server = socketserver.tcpserver((host, port),mytcphandler) t = threading.thread(target=self._server.serve_forever) t.setdaemon(true) t.start() self._title="hello" self.makewidgets() def makewidgets(self): """ make time label. """ self._canvas = canvas(self, width=600, height=400) self._canvas.pack() class mytcphandler(socketserver.baserequesthandler): """ requesthandler class our server. instantiated once per connection server, , must override handle() method implement communication client. """ def handle(self): self.data = self.request.recv(1024).strip() print "{} wrote:".format(self.client_address[0]) print self.data #i want set above title "self.data" self.request.sendall(self.data.upper()) def main(): root = tk() #sw = statustable(root) #sw.pack(side=right) nw = network(root) nw.pack(side=top) root.mainloop() if __name__ == '__main__': main()
i still struggling this, current code below. have 2 questions.
1. ideally set ip address insider network class, possible? 2. appears self._server not have attribute "_title", 1 trying pass mytcphandler.
from tkinter import * import time import math import lockfile import functions import socketserver import threading class network(tk): def __init__(self,server): tk.__init__(self) self._server=server t = threading.thread(target=self._server.serve_forever) t.setdaemon(true) # don't hang on exit t.start() self.makewidgets() def makewidgets(self): """ make time label. """ self._canvas = canvas(self, width=600, height=400) self._canvas.pack() radius=20 l = label(self,text=self._server._title) l.pack(fill=x, expand=no, pady=2, padx=2) class mytcphandler(socketserver.baserequesthandler): def __init__(self): self._title=stringvar() self._title="test" def handle(self): # self.request tcp socket connected client self.data = self.request.recv(1024).strip() print "{} wrote:".format(self.client_address[0]) print self.data self._title="change" print "changed?" # send same data, upper-cased self.request.sendall(self.data.upper()) def main(): host, port = "10.0.1.08", 9999 print host # create server, binding localhost on port 9999 server = socketserver.tcpserver((host, port),mytcphandler) #sw = statustable(root) #sw.pack(side=right) nw = network(server) nw.mainloop()
since both classes in same process (and hence same memory space), why not use shared data. if worried data being overwritten threads, can add lock on data.
you consider making second class instance of first 1 -- in case, sharing data seamless. here discussion, might find useful: how share data between 2 classes
try (you can remove lock, if have 1 client , okay overwritten):
lock = threading.lock() data_title = "" class network(tk): def __init__(self,server): tk.__init__(self) self._server=server t = threading.thread(target=self._server.serve_forever) t.setdaemon(true) # don't hang on exit t.start() self.makewidgets() def makewidgets(self): """ make time label. """ print(dir(self._server)) self._canvas = canvas(self, width=600, height=400) self._canvas.pack() radius=20 lock.acquire() l = label(self,text=data_title) lock.release() l.pack(fill=x, expand=no, pady=2, padx=2) class mytcphandler(socketserver.baserequesthandler): def __init__(self): self._title=stringvar() self._title="test" def handle(self): # self.request tcp socket connected client self.data = self.request.recv(1024).strip() print "{} wrote:".format(self.client_address[0]) print self.data lock.acquire() self._title="change" lock.release() print "changed?" # send same data, upper-cased self.request.sendall(self.data.upper())
Comments
Post a Comment