commit
5ba41f5690
@ -0,0 +1,162 @@
|
||||
# ---> Python
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.pyc
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
.idea/
|
@ -0,0 +1,11 @@
|
||||
[[source]]
|
||||
url = "https://pypi.org/simple"
|
||||
verify_ssl = true
|
||||
name = "pypi"
|
||||
|
||||
[packages]
|
||||
|
||||
[dev-packages]
|
||||
|
||||
[requires]
|
||||
python_version = "3.8"
|
@ -0,0 +1,20 @@
|
||||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "7f7606f08e0544d8d012ef4d097dabdd6df6843a28793eb6551245d4b2db4242"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
"python_version": "3.8"
|
||||
},
|
||||
"sources": [
|
||||
{
|
||||
"name": "pypi",
|
||||
"url": "https://pypi.org/simple",
|
||||
"verify_ssl": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"default": {},
|
||||
"develop": {}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022/9/10 10:15
|
||||
# @Author : old tom
|
||||
# @File : fu_lang.py
|
||||
# @Project : Futool
|
||||
# @Desc : 字符串相关
|
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022/9/10 10:17
|
||||
# @Author : old tom
|
||||
# @File : fu_math.py
|
||||
# @Project : Futool
|
||||
# @Desc : 数学计算
|
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022/9/10 10:46
|
||||
# @Author : old tom
|
||||
# @File : fu_parser.py
|
||||
# @Project : Futool
|
||||
# @Desc : 解析器(CSV,JSON,NB文件)
|
@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022/8/31 23:34
|
||||
# @Author : old tom
|
||||
# @File : http_downloader.py
|
||||
# @Project : Futool
|
||||
# @Desc : 文件下载器
|
||||
import time
|
||||
|
||||
from futool.http.http_request import head
|
||||
from multiprocessing import Pool
|
||||
import urllib.request as req
|
||||
|
||||
|
||||
class HttpDownloader(object):
|
||||
"""
|
||||
HTTP 下载器
|
||||
"""
|
||||
|
||||
def __init__(self, pool=None):
|
||||
self.pool = Pool(16) if not pool else pool
|
||||
|
||||
def download(self, url, dst, chunk_size=1000):
|
||||
"""
|
||||
文件下,自动开启多线程
|
||||
:param url: 下载链接
|
||||
:param dst: 保存路径
|
||||
:param chunk_size: 文件块
|
||||
:return:
|
||||
"""
|
||||
is_support, content_length = HttpDownloader.is_support_range(url)
|
||||
if is_support:
|
||||
# 每个线程下载字节偏移量
|
||||
offset = self.fork(int(content_length), chunk_size)
|
||||
self.__join(offset, url, dst)
|
||||
else:
|
||||
print('无法获取Content-Length,使用单线程下载')
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def is_support_range(url):
|
||||
"""
|
||||
判断是否支持range请求
|
||||
:return:
|
||||
"""
|
||||
wrapper = head(url)
|
||||
header = wrapper.header()
|
||||
h_keys = header.keys()
|
||||
if 'Accept-Ranges' in h_keys and 'Content-Length' in h_keys and header['Accept-Ranges'] != 'none':
|
||||
return True, header['Content-Length']
|
||||
else:
|
||||
return False, 0
|
||||
|
||||
@staticmethod
|
||||
def fork(content_length: int, chunk_size):
|
||||
"""
|
||||
拆分线程
|
||||
:param chunk_size: 文件块大小
|
||||
:param content_length:
|
||||
:return:
|
||||
"""
|
||||
offset = []
|
||||
if content_length <= chunk_size:
|
||||
offset.append((0, content_length))
|
||||
else:
|
||||
for i in range(content_length // chunk_size):
|
||||
start_offset = chunk_size * i + 1
|
||||
end_offset = start_offset - 1 + chunk_size
|
||||
offset.append((0 if i == 0 else start_offset, end_offset))
|
||||
offset.append((chunk_size * (content_length // chunk_size), content_length))
|
||||
return offset
|
||||
|
||||
def __join(self, offset, url, dst):
|
||||
"""
|
||||
多线程下载
|
||||
:param offset:
|
||||
:param url:
|
||||
:param dst:
|
||||
:return:
|
||||
"""
|
||||
|
||||
def download_by_thread(part):
|
||||
_request = req.Request(url=url, headers={
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70",
|
||||
'Range': f'bytes:{part[0]}-{part[1]}'
|
||||
}, method='GET')
|
||||
response = req.urlopen(_request)
|
||||
with open(dst + f'.{time.time_ns()}', 'wb') as f:
|
||||
f.write(response.read())
|
||||
|
||||
self.pool.map(download_by_thread, offset)
|
||||
self.pool.close()
|
||||
self.pool.join()
|
@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022/8/29 23:14
|
||||
# @Author : old tom
|
||||
# @File : http_response.py
|
||||
# @Project : Futool
|
||||
# @Desc : 响应解析
|
||||
|
||||
from http.client import HTTPResponse
|
||||
from http.cookiejar import CookieJar
|
||||
import json
|
||||
|
||||
DEFAULT_ENCODING = 'UTF-8'
|
||||
|
||||
# 响应类型编码
|
||||
RESPONSE_CONTENT_ENCODING = "Content-Encoding"
|
||||
|
||||
# 压缩类型
|
||||
COMPRESS_TYPE = ('gzip', 'deflate', 'br')
|
||||
|
||||
|
||||
class ResponseWrapper(object):
|
||||
|
||||
def __init__(self, response: HTTPResponse, cookie: CookieJar = None):
|
||||
self.resp = response
|
||||
if cookie and len(cookie) > 0:
|
||||
self.cookie = cookie
|
||||
|
||||
def body(self, encoding=DEFAULT_ENCODING):
|
||||
return self.resp.read().decode(encoding)
|
||||
|
||||
def json_body(self, encoding=DEFAULT_ENCODING):
|
||||
return json.loads(self.body(encoding))
|
||||
|
||||
def status(self):
|
||||
return self.resp.status
|
||||
|
||||
def is_ok(self):
|
||||
st_code = self.resp.status
|
||||
return 200 <= st_code <= 300
|
||||
|
||||
def header(self, name=None):
|
||||
return self.resp.getheader(name) if name else self._parse_header_dict()
|
||||
|
||||
def _parse_header_dict(self):
|
||||
headers = self.resp.getheaders()
|
||||
header_dict = {}
|
||||
if headers:
|
||||
for h in headers:
|
||||
header_dict[h[0]] = h[1]
|
||||
return header_dict
|
||||
|
||||
def is_compress(self):
|
||||
"""
|
||||
是否压缩
|
||||
:return:
|
||||
"""
|
||||
return self.compress_type() in COMPRESS_TYPE
|
||||
|
||||
def compress_type(self):
|
||||
"""
|
||||
压缩格式
|
||||
:return:
|
||||
"""
|
||||
header = self.header()
|
||||
if RESPONSE_CONTENT_ENCODING in header.keys():
|
||||
res_content_encoding = header[RESPONSE_CONTENT_ENCODING]
|
||||
return res_content_encoding
|
||||
|
||||
def cookies(self):
|
||||
"""
|
||||
获取cookie
|
||||
:return:
|
||||
"""
|
||||
ck = {}
|
||||
if self.cookie:
|
||||
for item in self.cookie:
|
||||
ck[item.name] = item.value
|
||||
return ck
|
@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022/9/10 10:07
|
||||
# @Author : old tom
|
||||
# @File : test_fu_sys.py
|
||||
# @Project : Futool
|
||||
from unittest import TestCase
|
||||
from futool.system import fu_sys
|
||||
|
||||
|
||||
# @Desc :
|
||||
class Test(TestCase):
|
||||
def test_is_windows(self):
|
||||
rt = fu_sys.is_windows()
|
||||
self.assertTrue(rt)
|
||||
|
||||
def test_is_linux(self):
|
||||
rt = fu_sys.is_linux()
|
||||
self.assertFalse(rt)
|
||||
|
||||
def test_is_unix(self):
|
||||
rt = fu_sys.is_unix()
|
||||
self.assertFalse(rt)
|
||||
|
||||
def test_os_name(self):
|
||||
rt = fu_sys.os_name()
|
||||
self.assertIsNotNone(rt)
|
||||
|
||||
def test_host_name(self):
|
||||
rt = fu_sys.host_name()
|
||||
self.assertIsNotNone(rt)
|
||||
|
||||
def test_sys_user(self):
|
||||
rt = fu_sys.sys_user()
|
||||
self.assertIsNotNone(rt)
|
||||
|
||||
def test_sys_user_dir(self):
|
||||
rt = fu_sys.sys_user_dir()
|
||||
self.assertIsNotNone(rt)
|
||||
|
||||
def test_cpu_info(self):
|
||||
cpu_info = fu_sys.CpuInfo()
|
||||
self.assertIsNotNone(cpu_info.cpu_count())
|
||||
self.assertIsNotNone(cpu_info.cpu_architecture())
|
Loading…
Reference in new issue