forked from mangwang/beginning_python_source_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisting28-2.py
More file actions
113 lines (91 loc) · 3.31 KB
/
listing28-2.py
File metadata and controls
113 lines (91 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from xmlrpclib import ServerProxy, Fault
from server import Node, UNHANDLED
from client import randomString
from threading import Thread
from time import sleep
from os import listdir
import sys
import wx
HEAD_START = 0.1 # Seconds
SECRET_LENGTH = 100
class ListableNode(Node):
"""
An extended version of Node, which can list the files
in its file directory.
"""
def list(self):
return listdir(self.dirname)
class Client(wx.App):
"""
The main client class, which takes care of setting up the GUI and
starts a Node for serving files.
"""
def __init__(self, url, dirname, urlfile):
"""
Creates a random secret, instantiates a ListableNode with that secret,
starts a Thread with the ListableNode's _start method (making sure the
Thread is a daemon so it will quit when the application quits),
reads all the URLs from the URL file and introduces the Node to
them. Finally, sets up the GUI.
"""
self.secret = randomString(SECRET_LENGTH)
n = ListableNode(url, dirname, self.secret)
t = Thread(target=n._start)
t.setDaemon(1)
t.start()
# Give the server a head start:
sleep(HEAD_START)
self.server = ServerProxy(url)
for line in open(urlfile):
line = line.strip()
self.server.hello(line)
# Get the GUI going:
super(Client, self).__init__()
def updateList(self):
"""
Updates the list box with the names of the files available
from the server Node.
"""
self.files.Set(self.server.list())
def OnInit(self):
"""
Sets up the GUI. Creates a window, a text field, a button, and
a list box, and lays them out. Binds the submit button to
self.fetchHandler.
"""
win = wx.Frame(None, title="File Sharing Client", size=(400, 300))
bkg = wx.Panel(win)
self.input = input = wx.TextCtrl(bkg);
submit = wx.Button(bkg, label="Fetch", size=(80, 25))
submit.Bind(wx.EVT_BUTTON, self.fetchHandler)
hbox = wx.BoxSizer()
hbox.Add(input, proportion=1, flag=wx.ALL | wx.EXPAND, border=10)
hbox.Add(submit, flag=wx.TOP | wx.BOTTOM | wx.RIGHT, border=10)
self.files = files = wx.ListBox(bkg)
self.updateList()
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox, proportion=0, flag=wx.EXPAND)
vbox.Add(files, proportion=1,
flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=10)
bkg.SetSizer(vbox)
win.Show()
return True
def fetchHandler(self, event):
"""
Called when the user clicks the 'Fetch' button. Reads the
query from the text field, and calls the fetch method of the
server Node. After handling the query, updateList is called.
If the query is not handled, an error message is printed.
"""
query = self.input.GetValue()
try:
self.server.fetch(query, self.secret)
self.updateList()
except Fault, f:
if f.faultCode != UNHANDLED: raise
print "Couldn't find the file", query
def main():
urlfile, directory, url = sys.argv[1:]
client = Client(url, directory, urlfile)
client.MainLoop()
if __name__ == '__main__': main()