python使用http.client发送post请求

编程入门 行业动态 更新时间:2024-10-28 14:26:33

python使用<a href=https://www.elefans.com/category/jswz/34/1771445.html style=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请求

本文发布于:2023-12-03 12:44:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1655475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:http   python   post   client

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!