Python发送Post请求

# -*- coding: utf-8 -*-
import sys
import imp
import os
import logging

# 导入requests包
import requests
import json

imp.reload(sys)
# sys.setdefaultencoding('utf8')
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

logging.basicConfig(format="%(asctime)s %(message)s", datefmt='%Y-%m-%d  %H:%M:%S %a')


url = "https://ip:port/path"
# 请求头,字典类型
headers = {
    'Content-Type': 'application/json;charset=utf-8',
    'app-key': '123456',
    'staffCode': 'abcdef'
}
# 请求体,字典类型,发送时需要转为json格式字符串 !!!
data = {
    "city": "南京", 
    "specialty": "*", 
    "account_num": "[\"025-123456\"]"
} 

# headers必须为字典类型,data应该是json字符串:json.dumps()将字典解析为字符串
res = requests.post(url = url, headers = headers, data = json.dumps(data))
res.encoding='utf-8'

# print("发送的body:",res.request.body)
# print("response返回结果:",res.text)

ipStr = ''
if "200" = res.status_code:
    # res.json()将字符串转换为字典类型
    ips = res.json()['data']['atom_data']['OltProp'].keys()
    for ip in ips:
        ipStr = ipStr + ip + ' '

print("ipStr:", ipStr)

你可能感兴趣的:(python,开发语言)