通过form-data进行gzip格式文件上传
# coding=utf8
import requests
from uuid import uuid4
import os
from requests_toolbelt.multipart.encoder import MultipartEncoder
import sys
conn = requests.Session()
login_data = {
'username': username,
'passwd': password
}
headers = {'Content-Type': 'application/json'}
login_url = 'http://{}:{}/omp/api/v1/user/login'.format(server_ip, port)
response = conn.post(
url=login_url,
json=login_data,
headers=headers
)
if response.status_code != 200:
print(
'Login failed reason:{}'.format(response.content.decode()))
sys.exit(1)
file_name = 'UYUN-platform-smc-R16.tar.gz'
url='http://{}:{}/omp/api/v1/pkgs/upload'.format(server_ip, port)
import urllib
encoded_name = urllib.quote(file_name)
boundary = 'WebKitFormBoundary'+uuid4().hex[0:16]
header = {'Content-Type': 'multipart/form-data; boundary={0}'.format(boundary), 'charset': 'UTF-8',
'Accept-Encoding': 'gzip,deflate'}
import gzip
with open("C:\\Users\\Administrator\\Desktop\\"+file_name, 'rb') as f:
m = MultipartEncoder(
fields={'file': (encoded_name, f,
'application/gzip')}
)
decoded_m = m.to_string()
content = f.readlines()
print content
content = ''.join(content)
datas = '--{0}{1}Content-Disposition: form-data; name="file"; filename="{2}"{1}Content-Type: application/gzip{1}{1}{3}{1}--{0}--{1}'. \
format(boundary, '\r\n', file_name, content, boundary)
print datas
print header
response = conn.post(url,
data=decoded_m,
headers={'Content-Type': m.content_type,
'charset': 'UTF-8'},
verify=False)
print response.status_code, response.text
url='http://{}:{}/omp/api/v1/pkgs/analysis?fileName={}'.format(server_ip, port,file_name)
headers = {'Content-Type': 'application/json'}
response = conn.get(url,
headers=headers,
verify=False)
print response.status_code, response.text
浙公网安备 33010602011771号