編寫Python SDK代碼
工程目錄結(jié)構(gòu)
1
2
3
4
5
6
7
8
9
|
├──── easyhttp // SDK目錄 │ ├── __init__.py │ ├── https.py // http工具類 ├── tests // 單元測試目錄 │ ├── __init__.py │ ├── test_https.py // http單元測試 ├── README.md ├── requirements.txt //依賴包 └── setup.py //setuptools安裝 |
requirements.txt
1
|
requests==2.24.0 |
https.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# -*- 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: 參數(shù) :return: """ logging.info( "請求方式:GET, 請求url: %s , 請求參數(shù): %s " % (base_path + detail_path, params)) response = requests.get(base_path + detail_path, params = params) logging.info( "請求方式:GET, 請求url: %s , 請求參數(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請求 :param cls: :param base_path: 域名 :param detail_path: 接口詳情 :param params: 參數(shù) :return: """ logging.info( "請求方式:POST, 請求url: %s ,請求參數(shù): %s " % (base_path + detail_path, params)) response = requests.post(base_path + detail_path, data = params, headers = cls .headers) logging.info( "請求方式:POST, 請求url: %s , 請求參數(shù): %s , 結(jié)果:%s" % (base_path + detail_path, params, response)) return response |
test_https.py
1
2
3
4
5
6
7
8
9
10
11
|
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包
1
|
pip install twine |
編寫構(gòu)建工具setup.py進(jìn)行打包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# -*- coding:utf8 -*- """ @author: guodong.li @email: [email protected] @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ā)布包的版本序號 description = "easy use http" , # 發(fā)布包的描述信息 author = "guodong.li" , # 發(fā)布包的作者信息 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ù)簡單介紹如下:
- –name 包名稱
- –version (-V) 包版本
- –author 程序的作者
- –author_email 程序的作者的郵箱地址
- –maintainer 維護(hù)者
- –maintainer_email 維護(hù)者的郵箱地址
- –url 程序的官網(wǎng)地址
- –license 程序的授權(quán)信息
- –description 程序的簡單描述
- –long_description 程序的詳細(xì)描述
- –platforms 程序適用的軟件平臺列表
- –classifiers 程序的所屬分類列表
- –keywords 程序的關(guān)鍵字列表
- –packages 需要處理的包目錄(包含__init__.py的文件夾)
- –py_modules 需要打包的python文件列表
- –download_url 程序的下載地址
- –data_files 打包時需要打包的數(shù)據(jù)文件,如圖片,配置文件等
- –scripts 安裝時需要執(zhí)行的腳步列表
- –package_dir 告訴setuptools哪些目錄下的文件被映射到哪個源碼包。一個例子:package_dir = {'': ‘lib'},表示“root package”中的模塊都在lib 目錄中。
- –requires 定義依賴哪些模塊
- –provides 定義可以為哪些模塊提供依賴
- –find_packages() 對于簡單工程來說,手動增加packages參數(shù)很容易,剛剛我們用到了這個函數(shù),它默認(rèn)在和setup.py同一目錄下搜索各個含有 init.py的包。其實我們可以將包統(tǒng)一放在一個src目錄中,另外,這個包內(nèi)可能還有aaa.txt文件和data數(shù)據(jù)文件夾。還可以排除一些特定的包find_packages(exclude=[".tests", ".tests.", "tests.", “tests”])
- –install_requires = [“requests”] 需要安裝的依賴包
- –entry_points 動態(tài)發(fā)現(xiàn)服務(wù)和插件
新增.pypirc文件
touch ~/.pypirc
在.pypirc文件添加如下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[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
1
|
python setup.py sdist bdist_wheel upload - r nexus |
或者打包命令和上傳命令分開操作
1、打包命令
1
|
python setup.py sdist bdist_wheel |
2、上傳命令
1
|
twine upload - r nexus dist / * # -r 可以選擇倉庫地址 |
創(chuàng)建虛擬環(huán)境,并下載私服包進(jìn)行驗證
創(chuàng)建虛擬環(huán)境
1
|
virtualenv - p / usr / bin / python venv |
激活虛擬環(huán)境
1
|
source venv / bin / activate |
下載包
1
|
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
代碼驗證
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> 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就已經(jīng)制作完成,并且實現(xiàn)了SDK到私服的上傳與下載。
到此這篇關(guān)于Python SDK實現(xiàn)私服上傳下載的示例的文章就介紹到這了,更多相關(guān)Python SDK私服上傳下載內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/m0_64519234/article/details/121620800