I’m learning Python and trying to test some rest web_service applications. From the below code what I’m trying to do is to post something and to get the result. When I launch this the console gives me this:
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
I think it’s because I don’t have this restful_lib module. where can I get it and how can I install it?
also, I want to know if the launch is good how can I get the returned data?
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('----==============================---- ')
The
restful_libcomes from http://code.google.com/p/python-rest-client, but its last change is 4 years ago, so I wouldn’t suggest using this package.I would strongly recommend the
requestslibrary for such a task. A POST request with XML data would simply look like this: