python3 服务端使用CGI脚本处理POST的Json数据

发送端代码

#!/usr/bin/env python3
#coding=utf-8
#agent.py
import requests
import json
import urllib.request

url='http://127.0.0.1:8000/cgi-bin/post.py' 
data={'openid':'fuyou','to_groupid':'yes'}  
data = json.dumps(data)  
data=bytes(data,'utf8')  
request=urllib.request.Request(url)
request.method='POST'
request.data = data
request.add_header('Content-Type','application/json')
with urllib.request.urlopen(request) as r:
	print()
	print(r.read().decode('utf-8'))

服务器端(接收端)

#!/usr/bin/env python3
#coding=utf-8
#/cgi-bin/post.py
import os,sys,json
con = int(os.environ["CONTENT_LENGTH"])
req_body = sys.stdin.read(con)
my_dict = json.loads(req_body)
print('Content-Type: text/html; charset=utf-8\n')
print("Hello, World!")
print("vv:{0}".format(my_dict['openid']))
print("vv:{0}".format(my_dict))
print(my_dict)

在/cgi-bin 目录的父目录,运行

$ python3 -m http.server --cgi 8000

另开终端

$ python3 agent.py 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JavaPub-rodert

谢谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值