Python SDK實現私服上傳下載的示例
編寫Python SDK代碼
工程目錄結構
├──── easyhttp // SDK目錄 │ ├── __init__.py │ ├── https.py // http工具類 ├── tests // 單元測試目錄 │ ├── __init__.py │ ├── test_https.py // http單元測試 ├── README.md ├── requirements.txt //依賴包 └── setup.py //setuptools安裝
requirements.txt
requests==2.24.0
https.py
# -*- coding:utf8 -*- """ @Project: easyhttp @File: https.py @Version: v1.0.0 @Time: 2020/6/24 17:22 @Author: guodong.li @Description: http """ from typing import Optional import requests import logging from requests import Response logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s', level=logging.DEBUG) class HttpUtils: headers = { "Content-Type": "application/json" } # http://10.193.199.44:5610/api/v1/manual/sleep?time=0 @staticmethod def base_get(base_path: str='', detail_path: str='', params: Optional[dict]=None)-> Response: """ GET請求 :param base_path: 域名 :param detail_path: 接口詳情 :param params: 參數 :return: """ logging.info("請求方式:GET, 請求url: %s , 請求參數: %s " % (base_path + detail_path, params)) response = requests.get(base_path + detail_path, params=params) logging.info("請求方式:GET, 請求url: %s , 請求參數: %s , 結果:%s" % (base_path + detail_path, params, response)) return response @classmethod def base_post(cls, base_path: str='', detail_path: str='', params: Optional[dict]=None)-> Response: """ POST請求 :param cls: :param base_path: 域名 :param detail_path: 接口詳情 :param params: 參數 :return: """ logging.info("請求方式:POST, 請求url: %s ,請求參數: %s " % (base_path + detail_path, params)) response = requests.post(base_path + detail_path, data=params, headers=cls.headers) logging.info("請求方式:POST, 請求url: %s , 請求參數: %s , 結果:%s" % (base_path + detail_path, params, response)) return response
test_https.py
import requests import logging from easyhttp.https import HttpUtils logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s', level=logging.DEBUG) r = requests.get("http://xxx.xxx.xxx.xxx:5610/api/v1/manual/sleep?time=0") logging.info(r) # <Response [200]> logging.info(type(r)) # <class 'requests.models.Response'> logging.info(r.status_code) # 200
代碼寫完瞭之後,打包並上傳到私服。
打包並上傳私服
安裝twine包
pip install twine
編寫構建工具setup.py進行打包
# -*- coding:utf8 -*- """ @author: guodong.li @email: [email protected] @time: 2019/7/31 14:04 @file: setup.py @desc: """ # 引入構建包信息的模塊 from setuptools import setup, find_packages try: # for pip >= 10 from pip._internal.req import parse_requirements from pip._internal.network.session import PipSession except ImportError: # for pip <= 9.0.3 from pip.req import parse_requirements from pip.download import PipSession # parse_requirements() returns generator of pip.req.InstallRequirement objects install_reqs = parse_requirements('requirements.txt', session=PipSession()) # reqs is a list of requirement # e.g. ['django==1.5.1', 'mezzanine==1.4.6'] reqs = [str(ir.req) for ir in install_reqs] # 定義發佈的包文件的信息 setup( name="easyhttp", # 發佈的包的名稱 version="1.0.0", # 發佈包的版本序號 description="easy use http", # 發佈包的描述信息 author="guodong.li", # 發佈包的作者信息 author_email="[email protected]", # 作者的聯系郵箱 packages=["easyhttp"], # include_package_data=True, # include everything in source control # ...but exclude README.txt from all packages exclude_package_data={'': ['README.md'], 'tests': ['*.py']}, install_requires=reqs, )
setup.py各參數簡單介紹如下:
- –name 包名稱
- –version (-V) 包版本
- –author 程序的作者
- –author_email 程序的作者的郵箱地址
- –maintainer 維護者
- –maintainer_email 維護者的郵箱地址
- –url 程序的官網地址
- –license 程序的授權信息
- –description 程序的簡單描述
- –long_description 程序的詳細描述
- –platforms 程序適用的軟件平臺列表
- –classifiers 程序的所屬分類列表
- –keywords 程序的關鍵字列表
- –packages 需要處理的包目錄(包含__init__.py的文件夾)
- –py_modules 需要打包的python文件列表
- –download_url 程序的下載地址
- –data_files 打包時需要打包的數據文件,如圖片,配置文件等
- –scripts 安裝時需要執行的腳步列表
- –package_dir 告訴setuptools哪些目錄下的文件被映射到哪個源碼包。一個例子:package_dir = {”: ‘lib’},表示“root package”中的模塊都在lib 目錄中。
- –requires 定義依賴哪些模塊
- –provides 定義可以為哪些模塊提供依賴
- –find_packages() 對於簡單工程來說,手動增加packages參數很容易,剛剛我們用到瞭這個函數,它默認在和setup.py同一目錄下搜索各個含有 init.py的包。其實我們可以將包統一放在一個src目錄中,另外,這個包內可能還有aaa.txt文件和data數據文件夾。還可以排除一些特定的包find_packages(exclude=[“.tests”, “.tests.”, “tests.”, “tests”])
- –install_requires = [“requests”] 需要安裝的依賴包
- –entry_points 動態發現服務和插件
新增.pypirc文件
touch ~/.pypirc
在.pypirc文件添加如下配置
[distutils] index-servers = pypi nexus [pypi] repository:https://pypi.python.org/pypi username:your_username password:your_password [nexus] repository=http://192.168.12.196:8081/repository/mypypi-hosted/ username=your_username password=your_password
打包並上傳至私服倉庫nexus
python setup.py sdist bdist_wheel upload -r nexus
或者打包命令和上傳命令分開操作
1、打包命令
python setup.py sdist bdist_wheel
2、上傳命令
twine upload -r nexus dist/* # -r 可以選擇倉庫地址
創建虛擬環境,並下載私服包進行驗證
創建虛擬環境
virtualenv -p /usr/bin/python venv
激活虛擬環境
source venv/bin/activate
下載包
pip install easyhttp==1.0.0 -i http://your_username:[email protected]:8081/repository/mypypi-hosted/simple/ --trusted-host 192.168.12.196
進入python shell環境
python
代碼驗證
>>> from pai.utils.https import HttpUtils >>> import logging >>> logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',level=logging.INFO) >>> r = requests.get("http://10.xxx.xxx.xxx:5610/api/v1/manual/sleep?time=0") 2020-07-02 11:31:50,903 - /root/python/20200702/venv/lib/python3.7/site-packages/urllib3/connectionpool.py[line:230] - DEBUG: Starting new HTTP connection (1): 10.xxx.xxx.xxx:5610 2020-07-02 11:31:51,065 - /root/python/20200702/venv/lib/python3.7/site-packages/urllib3/connectionpool.py[line:442] - DEBUG: http://10.xxx.xxx.xxx:5610 "GET /api/v1/manual/sleep?time=0 HTTP/1.1" 200 None >>> logging.info(r) # <Response [200]> 2020-07-02 11:32:15,420 - <stdin>[line:1] - INFO: <Response [200]> >>> >>> logging.info(type(r)) # <class 'requests.models.Response'> 2020-07-02 11:32:27,371 - <stdin>[line:1] - INFO: <class 'requests.models.Response'> >>> logging.info(r.status_code) # 200 2020-07-02 11:32:39,069 - <stdin>[line:1] - INFO: 200
至此,一個簡單的Python SDK就已經制作完成,並且實現瞭SDK到私服的上傳與下載。
到此這篇關於Python SDK實現私服上傳下載的示例的文章就介紹到這瞭,更多相關Python SDK私服上傳下載內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Python解析m3u8拼接下載mp4視頻文件的示例代碼
- 10 個Python中Pip的使用技巧分享
- 關於python中的setup.py
- Python軟件包安裝的三種常見方法
- Python網絡爬蟲之獲取網絡數據