urllib

urllib

post

 一、如同postman的数据在请求体中,并为json数据,并不在URL中

import urllib
import json
from urllib.request import urlopen
from urllib.request import Request
from urllib.parse import urlencode

# 准备url,拼接好
login_url = 'http://{server}:{port}{login_url}'.format(**Params)

# 准备登录数据
dict_data = {}
dict_data['username'] = Params['username']
dict_data['password'] = Params['password']

json_data = json.dumps(dict_data)       # dict -> json字符串, 要发送的数据是json数据

# 调整请求格式
data = bytes(json_data, 'utf8')     # json -> bytes     b'{"username": "asafx", "password": "qyskhfa"}'

request = Request(url=login_url, data=data, headers=Params['headers'])    # 处理成为一个Request实例对象,urllib.request.Request object
response = urlopen(request)     # 获取响应

json_data = response.read().decode('utf-8')      # 读取数据为bytes,然后解码为json字符串
dict_data = json.loads(json_data)           # dict字典数据

草稿:

 

你可能感兴趣的:(Django开发-Python)