Server with gui interface

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 56
Reputation: willygstyle is an unknown quantity at this point 
Solved Threads: 6
willygstyle willygstyle is offline Offline
Junior Poster in Training

Server with gui interface

 
0
  #1
Oct 30th, 2009
Hey guys, I got a little bored and thought I would to make some of the basic Client/Server applications I have a little bit flashier with a gui interface. So far I have just been working with the server and the main problem that I run into is when the program has to enter a loop while waiting for a response the gui starts to hang. The work around I have been using so far which quite frankly sucks, is to print a response while the exeption is being raised. It keeps the gui from hanging but it's quite annoying. The other thing I tried was to have it sleep for a short period of time but the gui seems to really hate that. Is there a more common or better way to handle this? Here is the code I'm working with at the moment, sorry I do relize it's pretty sloppy right now. I promise to clean it up a bit once I can get it functioning a little better .

  1. from tkinter import *
  2. from threading import Thread
  3. from time import sleep
  4. import socket
  5.  
  6. class Gui():
  7. def __init__(self, master):
  8.  
  9. self.running = 0 #not listening
  10. self.addr = ""
  11. self.conn = ""
  12.  
  13. self.frame = Frame(master)
  14. self.frame.pack(side = LEFT, anchor = N)
  15.  
  16. self.startb = Button(self.frame, text = "Start", command = self.startc)
  17. self.startb.pack(side = LEFT, anchor = N)
  18.  
  19. self.stopb = Button(self.frame, text = "Stop", command = self.stopc)
  20. self.stopb.pack(side = LEFT, anchor = N)
  21.  
  22. self.connectionl = Label(self.frame, text = "not connected")
  23. self.connectionl.pack(side = LEFT, anchor = SW)
  24.  
  25.  
  26. def startc(self):
  27. if self.running == 0:
  28. self.running += 1
  29. self.ls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  30. port = 9999
  31. self.ls.bind(('', port))
  32. print("Server listening on port %s" %port)
  33. self.ls.listen(1)
  34. self.ls.settimeout(.05)
  35. while self.running != 0:
  36. try:
  37. (self.conn, self.addr) = self.ls.accept()
  38. print("client is at", self.addr[0], "on port", self.addr[1])
  39. self.running = 0
  40. except:
  41. print("attempting to connect")#hangs without this
  42. if self.conn != "":
  43. self.rc = ""
  44. while self.rc != "done":
  45. try:
  46. self.rc = self.conn.recv(100).decode('utf-8')
  47. print("got command", self.rc)
  48. except:
  49. sleep(.05)
  50. print("no info sent yet")#hangs without this
  51. self.conn.close()
  52. else:
  53. self.running = 0
  54. print("Server not listening")
  55.  
  56. def stopc(self):
  57. self.running = 0
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. root = Tk()
  65. gui = Gui(root)
  66. root.mainloop()
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,474
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
Nov 1st, 2009
Don't know on TkIntering,
but if you're serious with your app, grab wxPython and MySQL-python. Make a client that will access remotely the mysql server, authenticate it and populate data/delete depending on whether they are admins or not

After that exercise, you will be fresh for a rest
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Python Forum


Views: 198 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC