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

📄 listing28-1.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 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 Node with that secret,        starts a Thread with the Node'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.        """        super(Client, self).__init__()        self.secret = randomString(SECRET_LENGTH)        n = Node(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)    def OnInit(self):        """        Sets up the GUI. Creates a window, a text field, and a button, and        lays them out. Binds the submit button to self.fetchHandler.        """        win = wx.Frame(None, title="File Sharing Client", size=(400, 45))        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)        vbox = wx.BoxSizer(wx.VERTICAL)        vbox.Add(hbox, proportion=0, flag=wx.EXPAND)        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. If the query is not handled, an error message is        printed.        """        query = self.input.GetValue()        try:            self.server.fetch(query, self.secret)        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 + -