import sys
#make sure it works if executed locally
if __name__ == "__main__":
sys.path.append("..")
from csengine.utility import DebugUtility
import requests
import time
[docs]
class Uploader:
IP = "165.232.144.123"
PORT = "8000"
ENDPOINT = "/upload"
[docs]
def upload(self, file):
try:
#add the file as well as the upload token
files = {"files": open(file, 'rb'),
"token": "CamSkoolBugLogExpress"}
url = "http://" + self.IP + ":" + self.PORT + self.ENDPOINT
#build the request
r = requests.Request('POST', url, files=files)
prepared=r.prepare()
#workaround - remove the filename attribute from the token in the request body
token_origin = prepared.body[-150:].decode("utf-8")
token_fixed = token_origin.replace("name=\"token\"; filename=\"token\"", "name=\"token\"")
#add newlines in order to compensate for wrong content length
for i in range(len(token_origin) - len(token_fixed)):
token_fixed += "\n"
prepared.body = prepared.body[:-150] + token_fixed.encode("utf-8")
#send file
s = requests.Session()
s.send(prepared)
except Exception as e:
DebugUtility.Err("Error while uploading file: ", e)
if __name__ == "__main__":
ul = Uploader()
start = time.time()
ul.upload("__init__.py")
print("execution time: " + str(start - time.time()))