您好,登錄后才能下訂單哦!
本篇內容介紹了“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: liguodongiot@163.com @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="liguodongiot@163.com", # 作者的聯系郵箱 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:your_password@192.168.12.196: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怎么實現私服上傳下載”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。