我正在学习 Python 并尝试测试一些其他 web_service 应用程序。从下面的代码中,我想做的是发布一些东西并得到结果。当我启动它时,控制台给了我这个:
Traceback (most recent call last):
File "D:\workspaces_branch1\csse120\TestPythonProject\src\UTF-Rest-Client.py", line 9, in <module>
import restful_lib
ImportError: No module named restful_lib
我认为这是因为我没有这个 restful_lib 模块。我在哪里可以获得它以及如何安装它?另外,我想知道启动是否良好,如何获取返回的数据?
import argparse
#additional lib, need installation (see README)
import restful_lib
testQueueId = 0
# FUNCTIONS
#generate POST content
def postContent():
xmlData = "<testQueueRequestByNewnancy>"+ \
"<nancy>"+ \
"<nancyTargetId>" + args.nancyTargetID + "</nancyTargetId>"+ \
"<nancyUrl>" + args.nancyUrl + "</nancyUrl>"+ \
"<nancyVersion>" + args.nancyVersion + "</nancyVersion>"+ \
"<projectId>" + args.projectID + "</projectId>"+ \
"<MumID>" + args.MumID + "</MumID>"+ \
"</nancy>"+ \
"<user>"+ \
"<id>" + args.userID + "</id>"+ \
"</user>"+ \
"</testQueueRequestByNewnancy>"
printVerbose('xmlData =' + xmlData)
return xmlData
#this function will print text only if verbose is on
def printVerbose(text):
if args.verbose:
print(text)
#this function will instruct scheduler through REST API call to do new tests
def callRestApi(content):
printVerbose('calling REST scheduler API...')
#recreate complete URL
base_url = 'http://localhost:8080/test'
#ensure http:// is at the beginning
if (base_url[0:7].lower() != 'http://'):
base_url = 'http://' + base_url
printVerbose('url is ' + base_url)
conn = restful_lib.Connection(base_url, username=args.login, password=args.password)
headers = {'content-type':'text/json', 'accept':'text/json'}
conn.request_post("/bynewnancy", args={'q': 'Test'}, headers=headers)
#conn.request_get("/bynewnancy", args=content, returns=conn.ret, headers={'Accept': 'text/json'})
# MAIN
#definition des variables
if __name__ == '__main__':
#parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-v', dest='verbose', action='store_true')
parser.add_argument('-bt', '--nancyTargetID', required=True)
parser.add_argument('-bu', '--nancyUrl', required=True)
parser.add_argument('-bv', '--nancyVersion', required=False)
parser.add_argument('-p', '--projectID', required=True)
parser.add_argument('-s', '--MumID', required=True)
parser.add_argument('-u', '--userID', required=True)
#parser.add_argument('-url', '--httpURL', default='127.0.0.1')
#parser.add_argument('-port', '--httpPort', default='8080')
#parser.add_argument('-login', '--login', default='admin')
#parser.add_argument('-pwd', '--password', default='admin')
#parser.add_argument('-o', '--outputDir', default='.')
#args is considered as a global variable
args = parser.parse_args()
postContent = postContent()
callRestApi(postContent)
print('----==============================---- ')