python3发送http请求

  |   评论   |   浏览

背景

常用代码片段记录

方案

使用http.client

import http.client import urllib.parse conn = http.client.HTTPSConnection(host) conn.request(method='GET', url=url) response = conn.getresponse() content_type = response.getheader('Content-Type') body = response.read() conn.close()

使用requests

import requests a = {"website":"abeffect.com","user_name":"note"} url = "http://www.example.com" headers = {'Content-Type': 'application/json;charset=UTF-8'} res = requests.request("post",url,json=a,headers=headers) print(res.status_code) print(res.text)