Project description
darren_utils
一个功能丰富的Python工具库,包含加密解密、哈希计算、HTTP请求、字符串处理、时间处理和文件操作等多种实用功能。
安装
pip install darren_utils
功能模块
文件操作 (darren_file)
| 方法名 |
描述 |
参数 |
返回值 |
| file_exists |
检查文件是否存在 |
file_path (str): 文件路径 |
bool: 文件存在返回True,否则返回False |
| dir_exists |
检查目录是否存在 |
dir_path (str): 目录路径 |
bool: 目录存在返回True,否则返回False |
| file_is_use |
检查文件是否被占用 |
file_path (str): 文件路径 |
bool: 文件被占用返回True,否则返回False |
| file_open |
打开文件并返回文件句柄 |
file_path (str): 文件路径, mode (str): 文件打开模式,默认为'r'(只读) |
file object or int: 成功时返回文件句柄,失败时返回-1 |
| file_execute |
执行文件,如运行exe程序或用默认程序打开文件 |
file_path (str): 文件路径 |
subprocess.Popen or bool or int: 成功时返回进程对象或True,失败时返回-1 |
| file_locate |
在文件管理器中定位文件,打开文件所在目录并选中该文件 |
file_path (str): 文件路径 |
bool or int: 成功时返回True,失败时返回-1 |
| file_copy |
复制文件 |
src_path (str): 源文件路径, dest_path (str): 目标文件路径, overwrite (bool): 是否覆盖已存在的文件,默认为False |
bool or int: 成功时返回True,失败时返回-1 |
| file_rename |
重命名文件 |
old_name (str): 原文件名, new_name (str): 新文件名 |
bool or int: 成功时返回True,失败时返回-1 |
| file_enumerate |
枚举某个目录下的指定类型文件 |
directory (str): 欲寻找的目录, pattern (str): 欲寻找的文件名(如果寻找全部文件可以填入*.,或.txt只找txt文件, 多个后缀中间用" |
"隔开), with_path (bool): 是否带路径(默认为假; 真=带目录路径,如C:\012.txt; 假=不带,如 012.txt), sort_alpha (bool): 是否按字母排序(默认为假;真=按字母a-z排序 假=不排序), recursive (bool): 是否遍历子目录(默认为假) |
| file_size |
获取文件大小 |
file_path (str): 文件路径, unit (str): 返回大小的单位(B、KB、MB、GB),默认为M |
str or int: 成功时返回格式化的文件大小字符串,失败时返回-1 |
| file_get_extension |
获取文件扩展名 |
file_path (str): 文件路径 |
str or int: 成功时返回文件扩展名(如:.jpg),没有扩展名时返回空字符串,失败时返回-1 |
| file_get_directory |
获取文件所在目录路径 |
file_path (str): 文件路径 |
str or int: 成功时返回文件所在目录路径,失败时返回-1 |
| file_get_info |
获取文件信息 |
file_path (str): 文件路径 |
dict or int: 成功时返回包含文件信息的字典,失败时返回-1 |
| file_get_name |
获取文件名 |
file_path (str): 文件路径, with_extension (bool): 是否带后缀,默认为False,不带后缀;True为带后缀 |
str or int: 成功时返回文件名,失败时返回-1 |
| file_delete |
删除文件 |
file_path (str): 文件路径, to_trash (bool): 是否删除到回收站,默认为True(删除到回收站);False为彻底删除 |
bool or int: 成功时返回True,失败时返回-1 |
字符串处理 (darren_string)
| 方法名 |
描述 |
参数 |
返回值 |
| string_random_string |
生成随机字符串 |
length (int): 字符串长度 |
str: 随机字符串 |
| string_get_between |
获取两个字符串之间的内容 |
text (str): 原始文本, start (str): 起始字符串, end (str): 结束字符串 |
str: 两个字符串之间的内容 |
| string_get_left |
获取字符串左侧内容 |
text (str): 原始文本, separator (str): 分隔符 |
str: 分隔符左侧的内容 |
| string_get_right |
获取字符串右侧内容 |
text (str): 原始文本, separator (str): 分隔符 |
str: 分隔符右侧的内容 |
时间处理 (darren_time)
| 方法名 |
描述 |
参数 |
返回值 |
| time_get_timestamp |
获取当前时间戳 |
无 |
int: 当前时间戳 |
| time_random_timestamp |
生成随机时间戳 |
无 |
int: 随机时间戳 |
| time_format |
格式化时间戳 |
timestamp (int): 时间戳, format_str (str): 格式化字符串 |
str: 格式化后的时间字符串 |
HTTP请求 (darren_http)
| 方法名 |
描述 |
参数 |
返回值 |
| darren_http |
发送HTTP请求 |
url (str): 请求地址, method (str): 请求方法, headers (dict): 请求头, data (dict): 请求数据 |
dict: 响应结果 |
| darren_http_proxy |
发送带代理的HTTP请求 |
url (str): 请求地址, method (str): 请求方法, headers (dict): 请求头, data (dict): 请求数据, proxy (str): 代理地址 |
dict: 响应结果 |
日志处理 (darren_utils)
| 方法名 |
描述 |
参数 |
返回值 |
| save_log |
保存日志 |
log_content (str): 日志内容, log_file (str): 日志文件路径 |
bool: 保存成功返回True,否则返回False |
Cookie处理 (darren_http)
| 方法名 |
描述 |
参数 |
返回值 |
| cookie_dict_to_string |
将字典转换为Cookie字符串 |
cookie_dict (dict): Cookie字典 |
str: Cookie字符串 |
| cookie_string_to_dict |
将Cookie字符串转换为字典 |
cookie_string (str): Cookie字符串 |
dict: Cookie字典 |
| cookie_merge |
合并两个Cookie字典 |
cookie_dict1 (dict): 第一个Cookie字典, cookie_dict2 (dict): 第二个Cookie字典 |
dict: 合并后的Cookie字典 |
哈希算法 (darren_hash)
| 方法名 |
描述 |
参数 |
返回值 |
| hash_md5_string |
计算字符串的MD5哈希值 |
text (str): 输入字符串 |
str: MD5哈希值 |
| hash_md5_bytes |
计算字节数组的MD5哈希值 |
data (bytes): 输入字节数组 |
str: MD5哈希值 |
| hash_sha1_string |
计算字符串的SHA1哈希值 |
text (str): 输入字符串 |
str: SHA1哈希值 |
| hash_sha1_bytes |
计算字节数组的SHA1哈希值 |
data (bytes): 输入字节数组 |
str: SHA1哈希值 |
| hash_sha256_string |
计算字符串的SHA256哈希值 |
text (str): 输入字符串 |
str: SHA256哈希值 |
| hash_sha256_bytes |
计算字节数组的SHA256哈希值 |
data (bytes): 输入字节数组 |
str: SHA256哈希值 |
| hash_sha512_string |
计算字符串的SHA512哈希值 |
text (str): 输入字符串 |
str: SHA512哈希值 |
| hash_sha512_bytes |
计算字节数组的SHA512哈希值 |
data (bytes): 输入字节数组 |
str: SHA512哈希值 |
| hash_crc32_int |
计算数据的CRC32值 |
data (bytes): 输入数据 |
int: CRC32值 |
HMAC算法 (darren_hmac)
| 方法名 |
描述 |
参数 |
返回值 |
| hmac_md5 |
使用MD5算法计算HMAC |
key (str): 密钥, message (str): 消息 |
str: HMAC值 |
| hmac_sha1 |
使用SHA1算法计算HMAC |
key (str): 密钥, message (str): 消息 |
str: HMAC值 |
| hmac_sha256 |
使用SHA256算法计算HMAC |
key (str): 密钥, message (str): 消息 |
str: HMAC值 |
| hmac_sha512 |
使用SHA512算法计算HMAC |
key (str): 密钥, message (str): 消息 |
str: HMAC值 |
| hmac_sha3_256 |
使用SHA3-256算法计算HMAC |
key (str): 密钥, message (str): 消息 |
str: HMAC值 |
| hmac_sha3_512 |
使用SHA3-512算法计算HMAC |
key (str): 密钥, message (str): 消息 |
str: HMAC值 |
CRC32算法 (darren_rc4)
| 方法名 |
描述 |
参数 |
返回值 |
| hash_crc32_string |
计算字符串的CRC32值 |
text (str): 输入字符串 |
int: CRC32值 |
RC4加密 (darren_rc4)
| 方法名 |
描述 |
参数 |
返回值 |
| rc4_encrypt_string |
使用RC4算法加密字符串 |
key (str): 密钥, plaintext (str): 明文 |
str: 加密后的字符串 |
| rc4_decrypt_string |
使用RC4算法解密字符串 |
key (str): 密钥, ciphertext (str): 密文 |
str: 解密后的字符串 |
| RC4 |
RC4加密类 |
key (bytes): 密钥 |
RC4对象 |
AES加密 (darren_aes)
| 方法名 |
描述 |
参数 |
返回值 |
| AESCipher |
AES加密类 |
key (str): 密钥 |
AESCipher对象 |
| aes_encrypt_string |
使用AES算法加密字符串 |
key (str): 密钥, plaintext (str): 明文 |
str: 加密后的字符串 |
| aes_decrypt_string |
使用AES算法解密字符串 |
key (str): 密钥, ciphertext (str): 密文 |
str: 解密后的字符串 |
DES加密 (darren_des)
| 方法名 |
描述 |
参数 |
返回值 |
| DESCipher |
DES加密类 |
key (str): 密钥 |
DESCipher对象 |
| des_encrypt_string |
使用DES算法加密字符串 |
key (str): 密钥, plaintext (str): 明文 |
str: 加密后的字符串 |
| des_decrypt_string |
使用DES算法解密字符串 |
key (str): 密钥, ciphertext (str): 密文 |
str: 解密后的字符串 |
3DES加密 (darren_3des)
| 方法名 |
描述 |
参数 |
返回值 |
| TDESCipher |
3DES加密类 |
key (str): 密钥 |
TDESCipher对象 |
| tdes_encrypt_string |
使用3DES算法加密字符串 |
key (str): 密钥, plaintext (str): 明文 |
str: 加密后的字符串 |
| tdes_decrypt_string |
使用3DES算法解密字符串 |
key (str): 密钥, ciphertext (str): 密文 |
str: 解密后的字符串 |
JavaScript兼容3DES加密 (darren_3des_js)
| 方法名 |
描述 |
参数 |
返回值 |
| TDESCipher_JS |
JavaScript兼容的3DES加密类 |
key (str): 密钥 |
TDESCipher_JS对象 |
| tdes_encrypt_string_js |
使用JavaScript兼容的3DES算法加密字符串 |
key (str): 密钥, plaintext (str): 明文 |
str: 加密后的字符串 |
| tdes_decrypt_string_js |
使用JavaScript兼容的3DES算法解密字符串 |
key (str): 密钥, ciphertext (str): 密文 |
str: 解密后的字符串 |
SM2加密 (darren_sm2)
| 方法名 |
描述 |
参数 |
返回值 |
| SM2Cipher |
SM2加密类 |
无 |
SM2Cipher对象 |
| sm2_generate_keypair |
生成SM2密钥对 |
无 |
tuple: (私钥, 公钥) |
| sm2_encrypt_string |
使用SM2算法加密字符串 |
public_key (str): 公钥, plaintext (str): 明文 |
str: 加密后的字符串 |
| sm2_decrypt_string |
使用SM2算法解密字符串 |
private_key (str): 私钥, ciphertext (str): 密文 |
str: 解密后的字符串 |
| sm2_sign |
使用SM2算法签名 |
private_key (str): 私钥, message (str): 消息 |
str: 签名 |
| sm2_verify |
使用SM2算法验证签名 |
public_key (str): 公钥, message (str): 消息, signature (str): 签名 |
bool: 验证成功返回True,否则返回False |
SM3哈希 (darren_sm3)
| 方法名 |
描述 |
参数 |
返回值 |
| hash_sm3_string |
计算字符串的SM3哈希值 |
text (str): 输入字符串 |
str: SM3哈希值 |
| hash_sm3_bytes |
计算字节数组的SM3哈希值 |
data (bytes): 输入字节数组 |
str: SM3哈希值 |
SM4加密 (darren_sm4)
| 方法名 |
描述 |
参数 |
返回值 |
| SM4Cipher |
SM4加密类 |
key (str): 密钥 |
SM4Cipher对象 |
| sm4_encrypt_string |
使用SM4算法加密字符串 |
key (str): 密钥, plaintext (str): 明文 |
str: 加密后的字符串 |
| sm4_decrypt_string |
使用SM4算法解密字符串 |
key (str): 密钥, ciphertext (str): 密文 |
str: 解密后的字符串 |
RSA加密 (darren_rsa)
| 方法名 |
描述 |
参数 |
返回值 |
| RSACipher |
RSA加密类 |
无 |
RSACipher对象 |
| rsa_generate_keypair |
生成RSA密钥对 |
key_size (int): 密钥长度 |
tuple: (私钥, 公钥) |
| rsa_encrypt_string |
使用RSA算法加密字符串 |
public_key (str): 公钥, plaintext (str): 明文, padding (str): 填充方式 |
str: 加密后的字符串 |
| rsa_decrypt_string |
使用RSA算法解密字符串 |
private_key (str): 私钥, ciphertext (str): 密文, padding (str): 填充方式 |
str: 解密后的字符串 |
| rsa_sign |
使用RSA算法签名 |
private_key (str): 私钥, message (str): 消息, hash_algorithm (str): 哈希算法 |
str: 签名 |
| rsa_verify |
使用RSA算法验证签名 |
public_key (str): 公钥, message (str): 消息, signature (str): 签名, hash_algorithm (str): 哈希算法 |
bool: 验证成功返回True,否则返回False |
JavaScript兼容RSA加密 (js_rsa)
| 方法名 |
描述 |
参数 |
返回值 |
| JSRSA |
JavaScript兼容的RSA加密类 |
无 |
JSRSA对象 |
| js_rsa_generate_keypair |
生成JavaScript兼容的RSA密钥对 |
key_size (int): 密钥长度 |
tuple: (私钥, 公钥) |
| js_rsa_public_encrypt |
使用公钥加密 |
public_key (str): 公钥, plaintext (str): 明文, padding (int or AsymmetricPadding): 填充方式 |
str: 加密后的字符串 |
| js_rsa_private_decrypt |
使用私钥解密 |
private_key (str): 私钥, ciphertext (str): 密文, padding (int or AsymmetricPadding): 填充方式 |
str: 解密后的字符串 |
| js_rsa_private_encrypt |
使用私钥加密 |
private_key (str): 私钥, plaintext (str): 明文, padding (int or AsymmetricPadding): 填充方式 |
str: 加密后的字符串 |
使用示例
import darren_utils
# 文件操作示例
if darren_utils.file_exists("example.txt"):
print("文件存在")
files = darren_utils.file_enumerate(".", "*.py")
print("Python文件列表:", files)
# 字符串处理示例
random_str = darren_utils.string_random_string(10)
print("随机字符串:", random_str)
# 时间处理示例
timestamp = darren_utils.time_get_timestamp()
print("当前时间戳:", timestamp)
# HTTP请求示例
response = darren_utils.darren_http("https://httpbin.org/get")
print("HTTP响应:", response)
# 哈希计算示例
md5_hash = darren_utils.hash_md5_string("hello world")
print("MD5哈希值:", md5_hash)
# 加密解密示例
encrypted = darren_utils.aes_encrypt_string("mykey", "hello world")
decrypted = darren_utils.aes_decrypt_string("mykey", encrypted)
print("AES加密解密:", decrypted)
许可证
MIT
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file darren_utils-0.1.3.tar.gz.
File metadata
-
Download URL: darren_utils-0.1.3.tar.gz
- Upload date:
-
Size: 31.0 kB
- Tags: Source
-
Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
Hashes for darren_utils-0.1.3.tar.gz
| Algorithm |
Hash digest |
|
| SHA256 |
2cd90dd46f27815a4335e04ca6eb907a027b09c9e625a76430c136db2c3b872b
|
|
| MD5 |
c58220c5ca35fc706b8ad21421697863
|
|
| BLAKE2b-256 |
0c91ffed9cc2e3ced2ce128b6b795eb7452bca414f600624f66ca97e0cf6390e
|
|
See more details on using hashes here.
File details
Details for the file darren_utils-0.1.3-py3-none-any.whl.
File metadata
-
Download URL: darren_utils-0.1.3-py3-none-any.whl
- Upload date:
-
Size: 36.4 kB
- Tags: Python 3
-
Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.13
File hashes
Hashes for darren_utils-0.1.3-py3-none-any.whl
| Algorithm |
Hash digest |
|
| SHA256 |
6dba04993f8e1c95dbaa3db6e1ba1df8f46475243af6e0f1b2e74d06d9f8c3d7
|
|
| MD5 |
610381a16aa4d30e88d42c00fef31a48
|
|
| BLAKE2b-256 |
e6a477237d594ad6bee3607c00820716b054c2515657c876ee7300f92b8e2479
|
|
See more details on using hashes here.