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
The sidebar is recommended
- Python from 0 to 1 (day 14) - Python conditional judgment 1
- Several very interesting modules in Python
- How IOS developers learn Python Programming 15 - object oriented programming 1
- Daily python, Chapter 20, exception handling
- Understand the basis of Python collaboration in a few minutes
- [centos7] how to install and use Python under Linux
- leetcode 1130. Minimum Cost Tree From Leaf Values(python)
- leetcode 1433. Check If a String Can Break Another String(python)
- Python Matplotlib drawing 3D graphics
- Talk about deep and shallow copying in Python
guess what you like
-
Python crawler series - network requests
-
Python thread 01 understanding thread
-
Analysis of earthquake distribution in the past 10 years with Python~
-
You need to master these before learning Python crawlers
-
After the old friend (R & D post) was laid off, I wanted to join the snack bar. I collected some data in Python. It's more or less a intention
-
Python uses redis
-
Python crawler - ETF fund acquisition
-
Detailed tutorial on Python operation Tencent object storage (COS)
-
[Python] comparison of list, tuple, array and bidirectional queue methods
-
Go Python 3 usage and pit Prevention Guide
Random recommended
- Python logging log error and exception exception callback method
- Learn Python quickly and take a shortcut~
- Python from 0 to 1 (day 15) - Python conditional judgment 2
- Python crawler actual combat, requests module, python to capture headlines and take beautiful pictures
- The whole activity collected 8 proxy IP sites to pave the way for the python proxy pool, and the 15th of 120 crawlers
- Why can't list be used as dictionary key value in Python
- Python from 0 to 1 (day 16) - Python conditional judgment 3
- What is the python programming language?
- Python crawler reverse webpack, a real estate management platform login password parameter encryption logic
- Python crawler reverse, a college entrance examination volunteer filling platform encrypts the parameter signsafe and decrypts the returned results
- Python simulated Login, selenium module, python identification graphic verification code to realize automatic login
- Python -- datetime (timedelta class)
- Python's five strange skills will bring you a sense of enrichment in mastering efficient programming skills
- [Python] comparison of dictionary dict, defaultdict and orderdict
- Test driven development using Django
- Face recognition practice: face recognition using Python opencv and deep learning
- leetcode 1610. Maximum Number of Visible Points(python)
- Python thread 03 thread synchronization
- Introduction and internal principles of Python's widely used concurrent processing Library Futures
- Python - progress bar artifact tqdm usage
- Python learning notes - the fifth bullet * class & object oriented
- Python learning notes - the fourth bullet IO operation
- Python crawler actual combat: crawl all the pictures in the answer
- Quick reference manual of common regular expressions, necessary for Python text processing
- [Python] the characteristics of dictionaries and collections and the hash table behind them
- Python crawler - fund information storage
- Python crawler actual combat, pyteseract module, python realizes the visualization of boos direct employment & hook post data
- Pit filling summary: Python memory leak troubleshooting tips
- Python code reading (Chapter 61): delaying function calls
- Through the for loop, compare the differences between Python and Ruby Programming ideas
- leetcode 1606. Find Servers That Handled Most Number of Requests(python)
- leetcode 1611. Minimum One Bit Operations to Make Integers Zero(python)
- 06python learning notes - reading external text data
- [Python] functions, higher-order functions, anonymous functions and function attributes
- Python Networkx practice social network visualization
- Data analysis starts from scratch, and pandas reads and writes CSV data
- Python review (format string)
- [pandas learning notes 01] powerful tool set for analyzing structured data
- leetcode 147. Insertion Sort List(python)
- apache2. 4 + windows deployment Django (multi site)