http.client发送post请求"/>
python使用http.client发送post请求
import http.client
import jsondef send_to_rest_api(data):# 设置目标URL和要发送的数据,URL为www.baidu/userhost = 'www.baidu'api_url = '/user'# 创建HTTP连接conn = http.client.HTTPSConnection(host) # 如果需要HTTPS,使用HTTPSConnection;如果是HTTP,使用HTTPConnection# 发送POST请求conn.request('POST', api_url, body=json.dumps(data), headers={'Content-Type': 'application/json'})# 获取响应response = conn.getresponse()# 读取响应内容response_data = response.read()# 打印响应内容if response.status == 200:api_res = json.loads(response_data.decode('utf-8'))else:# 在这里处理请求失败的情况print(f"Request failed with status code: {response.status}")conn.close()conn.close()if __name__ == '__main__':d = {"a": 0.0}send_to_rest_api(d)
更多推荐
python使用http.client发送post请求
发布评论