importosfrompathlibimportPathdefget_user_home(method='os'):ifmethod=='pathlib':returnPath.home()else:returnos.path.expanduser('~')if__name__=="__main__":method_choice=input("选择方法 ('os' 或 'pathlib'): ").strip().lower()user_home_path=get_user_home(method_choice)print(f"当前用...
# 获取下载路径downloads_path=user_home/"Downloads"print("下载路径:",downloads_path) 1. 2. 3. 3. 代码示例 我们可以将上述代码整合成一个完整的示例,用于展示如何获取用户路径及其子目录。 importosfrompathlibimportPathdefget_user_paths():# 使用 os 模块user_home_os=os.path.expanduser("~")desktop...
import requests import os url = 'https://www.example.com/image-page' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) image_urls = [] # 解...
"password":"123456"}#使用urlencode将字典参数序列化成字符串data_string=urllib.parse.urlencode(data_dict)#将序列化后的字符串转换成二进制数据,因为post请求携带的是二进制参数last_data=bytes(data_string,encoding='utf-8')print(last_data)#如果给urlopen这个函数传递了data这个参数,那么它的请求方式则不是g...
import os import winreg #判断回收站目录是否存在 def returnDir(): dirs = ['C:\\Recycler\\', 'C:\\Recycled', 'C:\\$Recycle.Bin\\'] for recycleDir in dirs: if os.path.isdir(recycleDir): return recycleDir return None #通过sid获取用户名信息 def sid2user(sid): try: key = winreg...
):defemit(self,record):logger_opt=logger.opt(depth=6,exception=record.exc_info)logger_opt.log(record.levelname,record.getMessage())defconfigure_logging(flask_app:Flask):"""配置日志"""path=Path(flask_app.config['LOG_PATH'])ifnot path.exists():path.mkdir(parents=True)log_name=Path(path,...
headers={'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}#在发送get请求时带上请求头和cookies resp=requests.get(url,headers=headers,cookies=cookies)print(resp.content.decode('utf-8')) ...
mcp= FastMCP("operateMysql")defget_db_config():"""从环境变量获取数据库配置信息 返回: dict: 包含数据库连接所需的配置信息 - host: 数据库主机地址 - port: 数据库端口 - user: 数据库用户名 - password: 数据库密码 - database: 数据库名称 ...
get("https://example.com", auth=("my_user", "password123")) # 验证方法一 auth = httpx.DigestAuth("my_user", "password123") # 验证方法二 httpx.get("https://example.com", auth=auth) 二、 客户端 1、 特性 如果您来自 Requests,httpx.Client()您可以使用它来代替requests.Session(). 其...
/usr/bin/python# -*- coding: UTF-8 -*-importosimporttimefile='/root/runoob.txt'# 文件路径print(os.path.getatime(file))# 输出最近访问时间print(os.path.getctime(file))# 输出文件创建时间print(os.path.getmtime(file))# 输出最近修改时间print(time.gmtime(os.path.getmtime(file)))# 以struct...