import xmlrpc.client
import sys
import json
import cmd
import urllib
from docopt import docopt, DocoptExit
from colorama import init, Fore
import time
classServerInfo(object):"""用来定义服务端信息"""
ServerIP =''
ServerPort =''@classmethoddefLoadServerInfo(cls):
cls.ServerIP ='127.0.0.1'
cls.ServerPort ='5000'@classmethoddefGetServerAddr(cls):"""用来获取服务器地址"""return(cls.ServerIP, cls.ServerPort)@classmethoddefGetServerUrl(cls):if(cls.ServerIP =='')or(cls.ServerPort ==''):return''return xmlrpc.client.ServerProxy('http://'+ cls.ServerIP
+':'+ cls.ServerPort
)# 定义一个装饰函数,用来解析命令行defdocopt_cmd(func):"""
This decorator is used to simplify the try/except block and pass the result
of the docopt parsing to the called action.
"""deffn(self, arg):try:
opt = docopt(fn.__doc__, arg)except DocoptExit as e:# The DocoptExit is thrown when the args do not match.# We print a message to the user and the usage block.print('Invalid Command!')print(e)returnexcept SystemExit:# The SystemExit exception prints the usage for --help# We do not need to do the print here.returnreturn func(self, opt)
fn.__name__ = func.__name__
fn.__doc__ = func.__doc__
fn.__dict__.update(func.__dict__)return fn
classMyInteractive(cmd.Cmd):"""该类用于提供命令行界面并和服务器进行交互"""# intro是欢迎界面
intro ="""***********************************
======Welcome to csdn ftz server-client!=======
(type help for a list of commands.)
***********************************"""# prompt是提示符
prompt ='ftz_csdn> '@docopt_cmddefdo_plusCaculate(self, arg):"""Usage:
plusCaculate --a=<int> --b=<int>
Options:
--a=<int> para 1
--b=<int> para 2
"""
s = ServerInfo().GetServerUrl()
a =int(arg['--a'])
b =int(arg['--b'])print(s.aPLusb(a,b))@docopt_cmddefdo_minusCaculate(self, arg):"""Usage:
minusCaculate --a=<int> --b=<int>
Options:
--a=<int> para 1
--b=<int> para 2
"""
s = ServerInfo().GetServerUrl()
a =int(arg['--a'])
b =int(arg['--b'])print(s.aMinusb(a,b))@docopt_cmddefdo_add_user(self, arg):"""Usage:
add_user --id=<string> --name=<string> --age=<int> --city=<string>
Options:
--id=<string> para 1
--name=<string> para 2
--age=<int> para 3
--city=<string> para 4
"""
s = ServerInfo().GetServerUrl()
i = arg['--id']
name = arg['--name']
age =int(arg['--age'])
city = arg['--city']
user =dict()
user["name"]=name
user["age"]=age
user["city"]=city
print(s.add_user(i, user))@docopt_cmddefdo_get_user(self, arg):"""Usage:
get_user --id=<string>
Options:
--id=<string> para 1
"""
s = ServerInfo().GetServerUrl()
i = arg['--id']print(s.get_user(i))@docopt_cmddefdo_show_user(self, arg):"""Usage:
show_user
"""
s = ServerInfo().GetServerUrl()print(s.show_user())@docopt_cmddefdo_update_user(self, arg):"""Usage:
update_user --id=<string> --name=<string> --age=<int> --city=<string>
Options:
--id=<string> para 1
--name=<string> para 2
--age=<int> para 3
--city=<string> para 4
"""
s = ServerInfo().GetServerUrl()
i = arg['--id']
name = arg['--name']
age =int(arg['--age'])
city = arg['--city']
user =dict()
user["name"]=name
user["age"]=age
user["city"]=city
print(s.update_user(i, user))@docopt_cmddefdo_del_user(self, arg):"""Usage:
del_user --id=<string>
Options:
--id=<string> para 1
"""
s = ServerInfo().GetServerUrl()
i = arg['--id']print(s.del_user(i))defdo_quit(self, arg):"""Quits out of Interactive Mode."""
sys.exit()defemptyline(self):print('ftz_csdn> ')if __name__ =='__main__':#init(autoreset=True)
ServerInfo().LoadServerInfo()
MyInteractive().cmdloop()