实现http请求的几种方式/curl/urllib

21 篇文章 0 订阅
3 篇文章 0 订阅

shell

  • 例1
//简单的post请求
 curl -d '{"key","value"}'  127.0.0.1:8080
  • 例2
//表单格式
curl -H 'Accept:application/json' -H 'charset:utf-8' -H 'X-Auth-Appid:10086' -H 'X-Auth-TimeStamp:1525687357' -H 'X-Auth-Sign:b3a7d66ed403b60aad0c0fdeec329089' -X POST -d 'xxx_id[]=35289&xxx_id[]''  127.0.0.1:8080
  • 例3
// json格式
 curl -i -H 'content-type: application/json' -X POST  -d '{}' 127.0.0.1:8080

python

  • 例1,稍微简单的一个例子,仅仅请求网页
from urllib.request import urlopen
s_raw_url = "www.xxx.com"
resp = urlopen(s_raw_url, timeout=1.5)

  • 例2
  • 数据格式:application/json,json格式
# -*- coding: utf-8 -*-
import sys
import json
import requests
def get_result(data):
    data = json.dumps(data,ensure_ascii=False).encode()
    headers = {'Content-Type': 'application/json'}
    url = ""
    result = requests.post(url,data=data,headers=headers)
    return result

def main():
    # json 数据格式
    data = {}
    result = get_result(data)
    print (result.content)
    
if __name__ == '__main__':
    main()
  • 例3
  • 数据格式:application/json,json格式
import urllib2
import urllib
import json
def GetCsspToken(send_data):
      # url为请求地址,这里以钉钉机器人为示例
      url = 'https://oapi.dingtalk.com/robot/send?access_token='
      req  = urllib2.Request(url, send_data, {'Content-Type':'application/json'})
      f = urllib2.urlopen(req)
      response = f.read()
      f.close()
  • 例4
  • 数据格式:multipart/form-data,多表单
import requests
import urllib
import json
from requests_toolbelt import MultipartEncoder
def file_post():
    #data为数据,s_file_path为文件路径,url为请求地址
    headers = { 'Content-Type': "multipart/form-data"}
    # 这里以多表单传文件示例
    m = MultipartEncoder(
        fields={
        "key": data["key"], 
        "signature": data["signature"], 
        "file": (s_file_path, open(s_file_path, 'rb'), 'application/octet-stream')
    })
    headers['Content-Type'] = m.content_type
    resp = requests.post(url, data=m, headers=headers, timeout=600)
    return resp.content
  • 例5
  • 数据格式:application/x-www-form-urlencoded,表单
def get_result():
    #数据
    send_data = {
        "data":data["data"]
    }
    #表单序列化
    send_data = url_encoder(send_data)
    headers = {
        'Content-Type': "application/x-www-form-urlencoded",
        'charset': "utf-8"
    }
    #url为请求地址
    result = requests.post(url, data=send_data, headers=headers)
    return res

常用方法

加密

  • 生成md5
'''
 * input: 字符串
 * ouput: md5值
'''
def MD5(src):
    import hashlib
    m2 = hashlib.md5()
    m2.update(src.encode('utf-8'))
    return  m2.hexdigest()
  • 表单序列化
'''
 * 表单序列化
 * input: 表单字典
 * 例:{“data”:"sth","state":1}
 * ouput: 序列化结果
 * 例:data=sth&state=1
'''
def url_encoder(params):
    g_encode_params = {}

    def _encode_params(params, p_key=None):
        encode_params = {}
        if isinstance(params, dict):
            for key in params:
                encode_key = '{}[{}]'.format(p_key,key)
                encode_params[encode_key] = params[key]
        elif isinstance(params, (list, tuple)):
            for offset,value in enumerate(params):
                encode_key = '{}[{}]'.format(p_key, offset)
                encode_params[encode_key] = value
        else:
            g_encode_params[p_key] = params

        for key in encode_params:
            value = encode_params[key]
            _encode_params(value, key)

    if isinstance(params, dict):
        for key in params:
            _encode_params(params[key], key)

    return urllib.parse.urlencode(g_encode_params)
  • 获取文件的二进制结果
def getByte(path):
    with open(path, 'rb') as f:
        img_byte = base64.b64encode(f.read())
    img_str = img_byte.decode('ascii')
    return img_str
  • 获取文件内容,类比PHP方法file_get_contents()
def file_get_contents(filename, use_include_path = 0, context = None, offset = -1, maxlen = -1):
    if (filename.find('://') > 0):
        ret = urllib.urlopen(filename).read()
        if (offset > 0):
            ret = ret[offset:]
        if (maxlen > 0):
            ret = ret[:maxlen]
        return ret
    else:
        fp = open(filename,'rb')
        try:
            if (offset > 0):
                fp.seek(offset)
            ret = fp.read(maxlen)
            return ret
        finally:
            fp.close( )

<<<未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值