88 views
Python / Python开发

封装python包上传至pypi

文章目录

准备工作

  • 注册pypi用户 pypi注册

  • 登录后,找到Add API token按钮,点击后填写信息可以生成一个 API token

  • (重要)在linux系统中,创建一个.pypirc文件,内容如下:

[pypi]
username = __token__
password = xxxx
# 生成API token 他会给你这些配置信息,参考官网 https://pypi.org/help/#apitoken

准备自定义包

  • 下面以我写的为例, 创建一个scannetwork.py的文件,内容如下
# -*- coding: utf-8 -*-
# @Author : ying
# @Time : 2020/8/12 8:46
# @File : scannetwork.py
# @Email : 249462971@qq.com
# @Purpose: 

import nmap, json


class ScanHosts(object):

    def __init__(self):
        self.nm = nmap.PortScanner()

    def scanHost(self, nets):
        self.nm.scan(nets, arguments='-n -sP -PE')
        all_host = self.nm.all_hosts()
        return all_host

    def allHost(self, nets):
        all_host = self.scanHost(nets)
        # print(all_host)
        nums = []
        for ip in all_host:
            num = int(ip.split('.')[-1])
            nums.append(num)
        nums.sort()
        range_num = [i for i in range(1, 255)]
        host_list = []
        for i in range_num:
            if i in nums:
                host_list.append(str(i) + ' Online ')
            else:
                host_list.append(str(i) + ' Offline ')
                # range_num.remove(i)
        net = nets.rsplit('.', 1)[0]
        status = [net + '.' + str(i) for i in host_list]

        return status

    def oneHost(self, nets):
        statu = self.scanHost(nets)
        if statu:
            statu = statu[0] + ' Online '
        else:
            statu = nets + ' Offline '
        status = []
        status.append(statu)
        # print(status)
        return status
  • 创建setup.py文件(重要)
from distutils.core import setup
from setuptools import setup, find_packages

setup(
    name='scannetwork',
    version='0.0.1',
    keywords=('simple', 'scannetwork'),
    description='just a scannetwork test',
    license='MIT License',
    url='http://blog.xiaoqying.com',
    author='ying',
    author_email='249462971@qq.com',
    install_requires=['python-nmap'],
    packages=find_packages(),
)
# 温馨提示1: url中一定要带http://或者https://,不然上传时会报400
# 温馨提示2: 写了author,就必须写author_email
# 温馨提示3: install_requires是依赖的包名称
  • 再创建两个说明文件README.md,LICENSE,内容自己填
  • 安装几个插件,用于打包上传
pip3 install twine
pip3 install wheel
  • 最终效果如下
scannetwork
├── __init__.py
├── LICENSE
├── README.md
├── scannetwork.py
└── setup.py

上传到pypi步骤

python3 setup.py bdist_wheel
twine upload dist/*

运行结果

[root@python ~]# cd scannetwork/
[root@python scannetwork]# python3 setup.py bdist_wheel
running bdist_wheel
running build
installing to build/bdist.linux-x86_64/wheel
running install
running install_egg_info
running egg_info
creating scannetwork.egg-info
writing scannetwork.egg-info/PKG-INFO
writing dependency_links to scannetwork.egg-info/dependency_links.txt
writing requirements to scannetwork.egg-info/requires.txt
writing top-level names to scannetwork.egg-info/top_level.txt
writing manifest file 'scannetwork.egg-info/SOURCES.txt'
reading manifest file 'scannetwork.egg-info/SOURCES.txt'
writing manifest file 'scannetwork.egg-info/SOURCES.txt'
Copying scannetwork.egg-info to build/bdist.linux-x86_64/wheel/scannetwork-0.0.1-py3.6.egg-info
running install_scripts
adding license file "LICENSE" (matched pattern "LICEN[CS]E*")
creating build/bdist.linux-x86_64/wheel/scannetwork-0.0.1.dist-info/WHEEL
creating 'dist/scannetwork-0.0.1-py3-none-any.whl' and adding 'build/bdist.linux-x86_64/wheel' to it
adding 'scannetwork-0.0.1.dist-info/LICENSE'
adding 'scannetwork-0.0.1.dist-info/METADATA'
adding 'scannetwork-0.0.1.dist-info/WHEEL'
adding 'scannetwork-0.0.1.dist-info/top_level.txt'
adding 'scannetwork-0.0.1.dist-info/RECORD'
removing build/bdist.linux-x86_64/wheel
[root@python scannetwork]# ls
build  dist  __init__.py  LICENSE  README.md  scannetwork.egg-info  scannetwork.py  setup.py
[root@python scannetwork]# twine upload dist/*
Uploading distributions to https://upload.pypi.org/legacy/
Uploading scannetwork-0.0.1-py3-none-any.whl
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.05k/4.05k [00:02<00:00, 1.72kB/s]
NOTE: Try --verbose to see response content.
HTTPError: 400 Bad Request from https://upload.pypi.org/legacy/
File already exists. See https://pypi.org/help/#file-name-reuse for more information.
# 因为之前上传过了,这里会提示已经存在

可以pip安装使用了

pip3 install scannetwork

>>> from scannetwork import scannetwork
>>> sh = scannetwork.
scannetwork.ScanHosts(  scannetwork.json        scannetwork.nmap        
>>> sh = scannetwork.ScanHosts()
>>> res = sh.
sh.allHost(   sh.nm         sh.oneHost(   sh.scanHost(  
>>> res = sh.oneHost('192.168.107.77')
>>> print(res)
['192.168.107.77 Online ']