Skip to content

Instantly share code, notes, and snippets.

@stask
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stask/9019700 to your computer and use it in GitHub Desktop.
Save stask/9019700 to your computer and use it in GitHub Desktop.
uploading files to practitest
# You will need to install MultipartPostHandler2 via pip
# $> sudo pip install MultipartPostHandler2
import MultipartPostHandler, urllib2, md5, time
URL = "https://prod.practitest.com/api/automated_tests/upload_test_result.json"
#
# Specify your API_KEY and API_SECRET_KEY below
API_KEY = ""
API_SECRET_KEY = ""
def auth():
ts = str(int(time.time()))
signature = md5.new(API_KEY + API_SECRET_KEY + ts).hexdigest()
return "custom api_key=" + API_KEY + ", signature=" + signature + ", ts=" + ts
def post():
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
headers = {
"Authorization": auth()
}
# you can upload multiple attachments
# in this example, we're uploading two files (one text and one png)
params = {
"project_id": "2",
"instance_display_id": "21:1",
"exit_code": "0",
"result": "text output\nwill be shown in 'Execution output' field\n",
"result_files[file0]": open("/tmp/file0.txt", "rb"),
"result_files[file1]": open("/tmp/file1.png", "rb")
}
req = urllib2.Request(URL, params, headers)
try:
res = opener.open(req)
print res.getcode()
except urllib2.URLError, e:
print e.code
print "Body: ", e.read()
post()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment