Python?SDK實(shí)現(xiàn)私服上傳下載的示例
編寫(xiě)Python SDK代碼
工程目錄結(jié)構(gòu)
├──── easyhttp // SDK目錄 │ ├── __init__.py │ ├── https.py // http工具類 ├── tests // 單元測(cè)試目錄 │ ├── __init__.py │ ├── test_https.py // http單元測(cè)試 ├── 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請(qǐng)求 :param base_path: 域名 :param detail_path: 接口詳情 :param params: 參數(shù) :return: """ logging.info("請(qǐng)求方式:GET, 請(qǐng)求url: %s , 請(qǐng)求參數(shù): %s " % (base_path + detail_path, params)) response = requests.get(base_path + detail_path, params=params) logging.info("請(qǐng)求方式:GET, 請(qǐng)求url: %s , 請(qǐng)求參數(shù): %s , 結(jié)果:%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請(qǐng)求 :param cls: :param base_path: 域名 :param detail_path: 接口詳情 :param params: 參數(shù) :return: """ logging.info("請(qǐng)求方式:POST, 請(qǐng)求url: %s ,請(qǐng)求參數(shù): %s " % (base_path + detail_path, params)) response = requests.post(base_path + detail_path, data=params, headers=cls.headers) logging.info("請(qǐng)求方式:POST, 請(qǐng)求url: %s , 請(qǐng)求參數(shù): %s , 結(jié)果:%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
代碼寫(xiě)完了之后,打包并上傳到私服。
打包并上傳私服
安裝twine包
pip install twine
編寫(xiě)構(gòu)建工具setup.py進(jìn)行打包
# -*- coding:utf8 -*- """ @author: guodong.li @email: liguodongiot@163.com @time: 2019/7/31 14:04 @file: setup.py @desc: """ # 引入構(gòu)建包信息的模塊 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] # 定義發(fā)布的包文件的信息 setup( name="easyhttp", # 發(fā)布的包的名稱 version="1.0.0", # 發(fā)布包的版本序號(hào) description="easy use http", # 發(fā)布包的描述信息 author="guodong.li", # 發(fā)布包的作者信息 author_email="liguodongiot@163.com", # 作者的聯(lián)系郵箱 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各參數(shù)簡(jiǎn)單介紹如下:
- –name 包名稱
- –version (-V) 包版本
- –author 程序的作者
- –author_email 程序的作者的郵箱地址
- –maintainer 維護(hù)者
- –maintainer_email 維護(hù)者的郵箱地址
- –url 程序的官網(wǎng)地址
- –license 程序的授權(quán)信息
- –description 程序的簡(jiǎn)單描述
- –long_description 程序的詳細(xì)描述
- –platforms 程序適用的軟件平臺(tái)列表
- –classifiers 程序的所屬分類列表
- –keywords 程序的關(guān)鍵字列表
- –packages 需要處理的包目錄(包含__init__.py的文件夾)
- –py_modules 需要打包的python文件列表
- –download_url 程序的下載地址
- –data_files 打包時(shí)需要打包的數(shù)據(jù)文件,如圖片,配置文件等
- –scripts 安裝時(shí)需要執(zhí)行的腳步列表
- –package_dir 告訴setuptools哪些目錄下的文件被映射到哪個(gè)源碼包。一個(gè)例子:package_dir = {'': ‘lib'},表示“root package”中的模塊都在lib 目錄中。
- –requires 定義依賴哪些模塊
- –provides 定義可以為哪些模塊提供依賴
- –find_packages() 對(duì)于簡(jiǎn)單工程來(lái)說(shuō),手動(dòng)增加packages參數(shù)很容易,剛剛我們用到了這個(gè)函數(shù),它默認(rèn)在和setup.py同一目錄下搜索各個(gè)含有 init.py的包。其實(shí)我們可以將包統(tǒng)一放在一個(gè)src目錄中,另外,這個(gè)包內(nèi)可能還有aaa.txt文件和data數(shù)據(jù)文件夾。還可以排除一些特定的包find_packages(exclude=[".tests", ".tests.", "tests.", “tests”])
- –install_requires = [“requests”] 需要安裝的依賴包
- –entry_points 動(dòng)態(tài)發(fā)現(xiàn)服務(wù)和插件
新增.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
打包并上傳至私服倉(cāng)庫(kù)nexus
python setup.py sdist bdist_wheel upload -r nexus
或者打包命令和上傳命令分開(kāi)操作
1、打包命令
python setup.py sdist bdist_wheel
2、上傳命令
twine upload -r nexus dist/* # -r 可以選擇倉(cāng)庫(kù)地址
創(chuàng)建虛擬環(huán)境,并下載私服包進(jìn)行驗(yàn)證
創(chuàng)建虛擬環(huán)境
virtualenv -p /usr/bin/python venv
激活虛擬環(huán)境
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
進(jìn)入python shell環(huán)境
python
代碼驗(yàn)證
>>> 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
至此,一個(gè)簡(jiǎn)單的Python SDK就已經(jīng)制作完成,并且實(shí)現(xiàn)了SDK到私服的上傳與下載。
到此這篇關(guān)于Python SDK實(shí)現(xiàn)私服上傳下載的示例的文章就介紹到這了,更多相關(guān)Python SDK私服上傳下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 如何使用七牛Python SDK寫(xiě)一個(gè)同步腳本及使用教程
- Python使用微信SDK實(shí)現(xiàn)的微信支付功能示例
- Idea安裝python顯示無(wú)SDK問(wèn)題解決方案
- 七牛云的python sdk 批量刪除資源的操作方法
- 關(guān)于Python調(diào)用百度語(yǔ)音合成SDK實(shí)現(xiàn)文字轉(zhuǎn)音頻的方法
- 如何解決PyCharm顯示:無(wú)效的Python?SDK
- 將Python代碼打包成可調(diào)用SDK的四種方法小結(jié)(適用于移動(dòng)端 App)
- VSCode設(shè)置python SDK路徑的實(shí)現(xiàn)步驟
相關(guān)文章
Python腳本實(shí)現(xiàn)Web漏洞掃描工具
這是去年畢設(shè)做的一個(gè)Web漏洞掃描小工具,主要針對(duì)簡(jiǎn)單的SQL注入漏洞、SQL盲注和XSS漏洞。下文給大家介紹了使用說(shuō)明和源代碼,一起看看吧2016-10-10在 Windows 下搭建高效的 django 開(kāi)發(fā)環(huán)境的詳細(xì)教程
這篇文章主要介紹了如何在 Windows 下搭建高效的 django 開(kāi)發(fā)環(huán)境,本文通過(guò)一篇詳細(xì)教程實(shí)例代碼相結(jié)合給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07快速進(jìn)修Python指南之面向?qū)ο蟾呒?jí)篇
這篇文章主要為大家介紹了Java開(kāi)發(fā)者如何快速進(jìn)修Python指南之面向?qū)ο蟾呒?jí)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Python、 Pycharm、Django安裝詳細(xì)教程(圖文)
這篇文章主要介紹了Python、 Pycharm、Django安裝詳細(xì)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04PyTorch模型創(chuàng)建與nn.Module構(gòu)建
這篇文章主要為大家介紹了PyTorch模型創(chuàng)建與nn.Module構(gòu)建示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

Python中g(shù)lob庫(kù)實(shí)現(xiàn)文件名的匹配

Python面向?qū)ο笏枷肱c應(yīng)用入門(mén)教程【類與對(duì)象】