Python调用get或post请求外部接口

import json
import requests


def request_post(url, param):
    fails = 0
    while True:
        try:
            if fails >= 20:
                break

            headers = {'content-type': 'application/json'}
            ret = requests.post(url, json=param, headers=headers, timeout=10)

            if ret.status_code == 200:
                text = json.loads(ret.text)
            else:
                continue
        except:
            fails += 1
            print('网络连接出现问题, 正在尝试再次请求: ', fails)
        else:
            break
    return text

if __name__ == '__main__':
    post_url = "http://127.0.0.1:9001/getSha1Test?message"
    request_param = {"username": "stuff4", "password": "1qaz@WSX"}
    a = request_post(post_url, request_param)
    print(a)

你可能感兴趣的:(Python随笔,post,python)