📄 listing27-3.py
字号:
from xmlrpclib import ServerProxy, Faultfrom cmd import Cmdfrom random import choicefrom string import lowercasefrom server import Node, UNHANDLEDfrom threading import Threadfrom time import sleepimport sysHEAD_START = 0.1 # SecondsSECRET_LENGTH = 100def randomString(length): """ Returns a random string of letters with the given length. """ chars = [] letters = lowercase[:26] while length > 0: length -= 1 chars.append(choice(letters)) return ''.join(chars)class Client(Cmd): """ A simple text-based interface to the Node class. """ prompt = '> ' def __init__(self, url, dirname, urlfile): """ Sets the url, dirname, and urlfile, and starts the Node Server in a separate thread. """ Cmd.__init__(self) 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 do_fetch(self, arg): "Call the fetch method of the Server." try: self.server.fetch(arg, self.secret) except Fault, f: if f.faultCode != UNHANDLED: raise print "Couldn't find the file", arg def do_exit(self, arg): "Exit the program." print sys.exit() do_EOF = do_exit # End-Of-File is synonymous with 'exit'def main(): urlfile, directory, url = sys.argv[1:] client = Client(url, directory, urlfile) client.cmdloop()if __name__ == '__main__': main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -