#!/usr/bin/python3
import pymysql
import requests
# 访问请求的方法
def requestUrl(result):
id = str(result['id'])
entName = str(result['entName'])
warnType = str(result['warnType'])
headers = {'Content-Type': 'application/json'}
#datas = json.dumps({"id":"202009151111","entName": "织里安木童衣有限公司","entChartered": "","warnType": "2","supervisorName": "韦云飞","phoneNumber": "18868213106","address": "","warnInfo":"测试","areaStreet": "","areaCountry": "","occurTime": "2020-09-15 15:26:55"})
param="id="+id+"&entName="+entName+"&entChartered&warnType="+warnType+"&supervisorName=韦云飞&phoneNumber=119&address&warnInfo=电流过大&areaStreet&areaCountry&occurTime=2020-07-28 20:30:21"
url="http://localhost:8080/park/insert?"+param
r = requests.post(url,headers=headers)
print(r.text)
#主函数用法
if __name__ == '__main__':
# 打开数据库连接
db = pymysql.connect("xxx.xx.xxx.xxx", "xxx", "xxx+", "xxx")
# 使用cursor()方法获取操作游标
cursor = db.cursor(cursor = pymysql.cursors.DictCursor)
# SQL 查询语句
sql = "SELECT * FROM alarm "
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for result in results:
requestUrl(result)
id = result[0]
entName = result[1]
# 打印结果
print("id=%s,entName=%s" % \
(id, entName))
total = cursor.rowcount
print("总数: %s" % \
(total))
print("处理完成")
except Exception as e:
print(e)
print("Error: unable to fetch data")
# 关闭数据库连接
db.close()
python send alarm_info
Python数据库操作与HTTP请求示例
最新推荐文章于 2024-03-24 11:23:29 发布
本文介绍了一个使用Python进行数据库操作并发送HTTP请求的例子。该程序通过pymysql库连接数据库,读取alarm表中的数据,并使用requests库向本地服务器发送POST请求。文章展示了如何构建动态URL参数及处理响应。
8191

被折叠的 条评论
为什么被折叠?



