zoukankan      html  css  js  c++  java
  • python表单流式上传gzip格式文件代码

    通过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, '
    ', 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
    

      

  • 相关阅读:
    缩放图片
    Volley下载图片存放在data/data下 networkImageView lrucache
    类实现Parcelable接口在Intent中传递
    基本控件设置边角图片 drawableleft
    屏幕全屏之类的问题
    关于点击按钮分享
    万能适配器的一些问题
    自定义控件高级
    Fragment 生命周期 全局变量的声明位置
    GridView
  • 原文地址:https://www.cnblogs.com/slqt/p/11124926.html
Copyright © 2011-2022 走看看