您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:Python

51自学网 2022-02-21 10:46:51
  python
这篇教程Python写得很实用,希望能帮到您。

编写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 Optionalimport requestsimport loggingfrom requests import Responselogging.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 requestsimport loggingfrom easyhttp.https import HttpUtilslogging.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_packagestry:  # for pip >= 10    from pip._internal.req import parse_requirements    from pip._internal.network.session import PipSessionexcept 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 objectsinstall_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各参数简单介绍如下:

万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。