python应用

requests

GET

python 复制代码
import requests

url ='https://finance.pae.baidu.com/vapi/stockshort'
params = {'code':'09888', 'market':'hk', 'finClientType':'pc' }
headers = {'User-Agent': 'Mozilla/5.0'}

response = requests.get(url, params=params, headers=headers)

print(response.status_code)
print(response.text)

POST

python 复制代码
# 表单格式
import requests

url = 'https://example.com/api'
data = {'key1': 'value1', 'key2': 'value2'}  # 表单数据

response = requests.post(url, data=data)

print(response.status_code)
print(response.json())
python 复制代码
# json格式
import requests

url = 'https://example.com/api'
json_data = {'key1': 'value1', 'key2': 'value2'}  # JSON 数据
headers = {'Content-Type': 'application/json'}

response = requests.post(url, json=data, headers=headers)

print(response.status_code)
print(response.json())
pymsql
python 复制代码
import pymysql
from pymysql import cursors

class SQLHelper:
    env = {
        'host': 'localhost',
        'user': 'root',
        'password': 'ccc+022599',
        'database': 'test'
    }

    @classmethod
    def open(cls):
        conn = pymysql.connect(host=cls.env['host'],
                               user=cls.env['user'],
                               password=cls.env['password'],
                               database=cls.env['database'])
        cursor = conn.cursor(cursor= pymysql.cursors.DictCursor)
        return conn, cursor

    @staticmethod
    def close(conn, cursor):
        cursor.close()
        conn.close()

    @classmethod
    def fetch_one(cls, sql, params):
        conn, cursor = SQLHelper.open()
        cursor.execute(sql, params)
        result = cursor.fetchone()
        SQLHelper.close(conn, cursor)

        return result

    @classmethod
    def insert_many(cls, sql, params):
        conn, cursor = SQLHelper.open()
        try:
            cursor.executemany(sql, params)
            affected_rows = cursor.rowcount
            conn.commit()
            return affected_rows
        except Exception as e:
            conn.rollback()
            print(e)
            return 0
        finally:
            SQLHelper.close(conn, cursor)
python 复制代码
if __name__ == '__main__':
    arr = [
        {'name':'ccb', 'age':25},
        {'name':'ccb2', 'age':26}
    ]
    
    sql = 'insert into emp (name, age) values (%s, %s)'
    params = [(item['name'], item['age']) for item in arr ]
    
    affected_rows = SQLHelper.insert_many(sql, params)
    print(affected_rows)
logging
python 复制代码
import logging
from logging.handlers import TimedRotatingFileHandler

# 配置日志记录器
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# 创建TimedRotatingFileHandler对象
handler = TimedRotatingFileHandler('app.log', when='midnight', interval=1, backupCount=7)
handler.suffix = '%Y-%m-%d'
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))

logger.addHandler(handler)

# 记录日志
logger.debug('这是一条DEBUG级别的日志')
logger.info('这是一条INFO级别的日志')
相关推荐
战族狼魂2 小时前
CSGO 皮肤交易平台后端 (Spring Boot) 代码结构与示例
java·spring boot·后端
杉之3 小时前
常见前端GET请求以及对应的Spring后端接收接口写法
java·前端·后端·spring·vue
hycccccch4 小时前
Canal+RabbitMQ实现MySQL数据增量同步
java·数据库·后端·rabbitmq
bobz9654 小时前
k8s 怎么提供虚拟机更好
后端
bobz9655 小时前
nova compute 如何创建 ovs 端口
后端
用键盘当武器的秋刀鱼5 小时前
springBoot统一响应类型3.5.1版本
java·spring boot·后端
Asthenia04126 小时前
从迷宫到公式:为 NFA 构造正规式
后端
Asthenia04126 小时前
像整理玩具一样:DFA 化简和状态等价性
后端
Asthenia04126 小时前
编译原理:打包思维-NFA 怎么变成 DFA
后端
非ban必选6 小时前
spring-ai-alibaba第五章阿里dashscope集成mcp远程天气查询tools
java·后端·spring