current position:Home>How to make a python SDK and upload and download private servers

How to make a python SDK and upload and download private servers

2022-02-01 10:56:17 Eat jelly without spitting jelly skin

This is my participation 11 The fourth of the yuegengwen challenge 26 God , Check out the activity details :2021 One last more challenge

In our daily work , Often need to interact with upstream and downstream , Especially when we do some basic services , Need to provide a for other departments to use , therefore , You need to write the corresponding SDK Upload to the company's private server , For other departments . today , Jelly will come with you to realize a simple Python SDK, And upload and download private servers .

To write Python SDK Code

Project directory structure

├──── easyhttp                   // SDK Catalog 
 │ ├── __init__.py │ ├── https.py // http Tool class  ├── tests //  Unit test directory  │ ├── __init__.py │ ├── test_https.py // http unit testing  ├── README.md ├── requirements.txt // Dependency package  └── setup.py //setuptools install   Copy code 

requirements.txt

requests==2.24.0
 Copy code 

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 request  :param base_path:  domain name  :param detail_path:  Interface details  :param params:  Parameters  :return: """
        logging.info(" Request mode :GET,  request url: %s ,  Request parameters : %s " % (base_path + detail_path, params))
        response = requests.get(base_path + detail_path, params=params)
        logging.info(" Request mode :GET,  request url: %s ,  Request parameters : %s ,  result :%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 request  :param cls: :param base_path:  domain name  :param detail_path:  Interface details  :param params:  Parameters  :return: """
        logging.info(" Request mode :POST,  request url: %s , Request parameters : %s " % (base_path + detail_path, params))
        response = requests.post(base_path + detail_path, data=params, headers=cls.headers)
        logging.info(" Request mode :POST,  request url: %s ,  Request parameters : %s ,  result :%s" % (base_path + detail_path, params, response))
        return response
 Copy code 

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
 Copy code 

After the code is written , Package and upload to private server .

Pack and upload private servers

install twine package

pip install twine
 Copy code 

Write build tools setup.py package

# -*- coding:utf8 -*-
""" @author: guodong.li @email: [email protected] @time: 2019/7/31 14:04 @file: setup.py @desc: """

#  Modules that introduce build package information 
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]

#  Define the information of the published package file 
setup(
    name="easyhttp",  #  The name of the published package 
    version="1.0.0",  #  Release package version number 
    description="easy use http",  #  Description of the release package 
    author="guodong.li",  #  Publish the author information of the package 
    author_email="[email protected]",  #  The author's contact email 
    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,
)
 Copy code 

setup.py The parameters are briefly introduced as follows :

  • --name Package name
  • --version (-V) Package version
  • --author Author of the program
  • --author_email The email address of the author of the program
  • --maintainer Maintainer
  • --maintainer_email Maintainer's email address
  • --url The official website address of the program
  • --license Authorization information of the program
  • --description A brief description of the program
  • --long_description A detailed description of the procedure
  • --platforms List of software platforms applicable to the program
  • --classifiers Classification list of the program
  • --keywords Keyword list of the program
  • --packages Package directory to process ( contain __init__.py Folder )
  • --py_modules Need to be packed python File list
  • --download_url Download address of the program
  • --data_files Data files that need to be packaged when packaging , Such as images , Configuration files, etc
  • --scripts List of steps to perform during installation
  • --package_dir tell setuptools Which directory files are mapped to which source package . An example :package_dir = {'': 'lib'}, Express “root package” All the modules in lib Directory .
  • --requires Define which modules to rely on
  • --provides Define which modules you can provide dependencies for
  • --find_packages() For a simple project , Manually add packages Parameters are easy , We just used this function , It's default between and setup.py Search the same directory containing init.py My bag . In fact, we can put all the packages in one src Directory , in addition , There may be... In this bag aaa.txt Document and data Data folder . You can also exclude some specific packages find_packages(exclude=[".tests", ".tests.", "tests.", "tests"])
  • --install_requires = ["requests"] Dependency packages that need to be installed
  • --entry_points Dynamic discovery services and plug-ins

newly added .pypirc file

touch ~/.pypirc

stay .pypirc Add the following configuration to the file

[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
 Copy code 

Package and upload to the private server warehouse nexus

python setup.py sdist bdist_wheel upload -r nexus

Or the package command and upload command can be operated separately

1、 Packing command

python setup.py sdist bdist_wheel

2、 Upload command

twine upload -r nexus dist/* # -r You can choose the warehouse address

Creating a virtual environment , And download the private server package for verification

Creating a virtual environment

virtualenv -p /usr/bin/python venv
 Copy code 

Activate the virtual environment

source venv/bin/activate
 Copy code 

Download package

pip  install easyhttp==1.0.0 -i http://your_username:[email protected]:8081/repository/mypypi-hosted/simple/  --trusted-host 192.168.12.196
 Copy code 

Get into python shell Environmental Science

python

Code validation

>>> 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
 Copy code 

thus , A simple Python SDK It has been made , And implemented SDK Upload and download to private server .

copyright notice
author[Eat jelly without spitting jelly skin],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011056151928.html

Random recommended