⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listing28-2.py

📁 《Beginning Python--From Novice to Professional》 的源码
💻 PY
字号:
from xmlrpclib import ServerProxy, Faultfrom server import Node, UNHANDLEDfrom client import randomStringfrom threading import Threadfrom time import sleepfrom os import listdirimport sysimport wxHEAD_START = 0.1 # SecondsSECRET_LENGTH = 100class 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", querydef main():    urlfile, directory, url = sys.argv[1:]    client = Client(url, directory, urlfile)    client.MainLoop()if __name__ == '__main__': main()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -