2 # -*- coding: utf-8 -*-
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
20 from os
.path
import getsize
, basename
21 from hashlib
import md5
23 __author__
= "Alfonso Tierno, alfonso.tiernosepulveda@telefonica.com"
24 __date__
= "$2018-01-01$"
26 version_date
= "Jan 2018"
30 print("Usage: ", sys
.argv
[0], "[options]")
31 print(" --version: prints current version")
32 print(" -f|--file FILE: file to be sent")
33 print(" -h|--help: shows this help")
34 print(" -u|--url URL: complete server URL")
35 print(" -s|--chunk-size SIZE: size of chunks, by default 1000")
36 print(" -t|--token TOKEN: Authorizaton token, previously obtained from server")
37 print(" -v|--verbose print debug information, can be used several times")
41 if __name__
== "__main__":
43 # load parameters and configuration
44 opts
, args
= getopt
.getopt(
47 ["url=", "help", "version", "verbose", "file=", "chunk-size=", "token="],
57 print("upload version " + __version__
+ " " + version_date
)
59 elif o
in ("-v", "--verbose"):
61 elif o
in ("-h", "--help"):
64 elif o
in ("-u", "--url"):
66 elif o
in ("-s", "--chunk-size"):
68 elif o
in ("-f", "--file"):
70 elif o
in ("-t", "--token"):
73 assert False, "Unhandled option"
74 total_size
= getsize(pkg_file
)
78 with
open(pkg_file
, "rb") as f
:
80 "Content-type": "application/gzip",
81 "Content-Filename": basename(pkg_file
),
82 "Accept": "application/json",
85 headers
["Authorization"] = token
86 while index
< total_size
:
87 chunk_data
= f
.read(chunk_size
)
88 file_md5
.update(chunk_data
)
89 # payload = {"file_name": pkg_file, "chunk_data": base64.b64encode(chunk_data).decode("utf-8"),
90 # "chunk_size": chunk_size}
92 headers
["Transaction-Id"] = transaction_id
93 if index
+ len(chunk_data
) == total_size
:
94 headers
["Content-File-MD5"] = file_md5
.hexdigest()
95 # payload["id"] = transaction_id
96 headers
["Content-range"] = "bytes {}-{}/{}".format(
97 index
, index
+ len(chunk_data
) - 1, total_size
99 # refers to rfc2616: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
101 print("TX chunk Headers: {}".format(headers
))
102 r
= requests
.post(url
, data
=chunk_data
, headers
=headers
, verify
=False)
103 if r
.status_code
not in (200, 201):
104 print("Got {}: {}".format(r
.status_code
, r
.text
))
107 print("RX {}: {}".format(r
.status_code
, r
.text
))
109 if not transaction_id
:
110 transaction_id
= response
["id"]
111 index
+= len(chunk_data
)
113 print("RX {}: {}".format(r
.status_code
, r
.text
))
115 print("---\nid: {}".format(response
["id"]))