使用pypi-server搭建简单的PyPI源

pypiserver 是一个最基本的PyPI服务器实现, 可以用来上传和维护python包. 本文介绍 pypiserver 在 AWS EC2 上的基本安装, 配置和使用.

基本安装和使用

安装和启动

pypiserver 可以在Python 2或者Python 3下运行. 使用pip就可以安装:

pip install pypiserver

启动 pypiserver 使用以下命令:

pypi-server

在没有显示指定任何启动参数的时候, pypiserver 是使用 ~/packages 来保存Python包, 同时监听8080端口来提供PyPI服务。

在安装和启动 pypiserver 后, 可以通过浏览器访问http://localhost:8080可以访问 pypiserver 的默认欢迎页:

我的测试过程

具体到该示例, pypi server 上我的目录结构如下:

pypi-server/
├── auth
├── packages
│   ├── full_month-0.0.4-py3-none-any.whl
│   └── full-month-0.0.4.tar.gz
└── pypi-env
    ├── ...

认证

For auth, we'll use basic authentication with htpasswd.

SSH back into the instance and install htpasswd via httpd-tools:

[ec2-user]$ sudo yum install -y httpd-tools

Create an "auth" folder:

[ec2-user]$ mkdir /home/ec2-user/pypi-server/auth
[ec2-user]$ cd /home/ec2-user/pypi-server/auth

# 创建的密码文件保存在 auth/ 文件夹中

Now, create your first user:

[ec2-user]$ htpasswd -sc .htpasswd <SOME-USERNAME>

Use htpasswd -s .htpasswd <SOME-USERNAME> for adding additional users.

启动 pypi-server 

pypi-server -p 8000 -P auth/.htpasswd -a update,download,list --fallback-url https://pypi.doubanio.com/simple ./packages

# -p 是端口; -P 密码文件; -a 表示需要认证的操作,该示例中 上传、下载安装和查看包都需要认证; fallback-url 表示如果的你的 packages 中没有对应的包时,去哪个源获取这个包;./packages 表示包的位置 

上传Python包

还是以上篇文章中的包为例,

twine upload --repository-url http://192.168.0.1:8000/ dist/*    # 192.168.0.1 是我示例中的 pypi server ip

# 由于启动 pypi-server -a 中包含了 upload,所以上传时需要输入用户名和密码

上传成功示例:

MacBook-Pro:full-month zhenxink$ twine upload --repository-url http://192.168.0.1:8000/ dist/*
Uploading distributions to http://192.168.0.1:8000/
Enter your username: neozheng
Enter your password: 
Uploading full_month-0.0.4-py3-none-any.whl
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████| 5.75k/5.75k [00:00<00:00, 32.7kB/s]
Uploading full-month-0.0.4.tar.gz
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████| 4.48k/4.48k [00:00<00:00, 24.9kB/s]
MacBook-Pro:full-month zhenxink$ 

# 上传需要输入用户名密码

用户下载安装 pypi server 的包

pip install --index-url http://192.168.0.1:8000/simple/ full-month --trusted-host 192.168.0.1

# 如果pypi server 不是 https , 需要加上 trusted-host

安装成功示例:

MacBook-Pro:aws zhenxink$ pip install --index-url http://192.168.0.1:8000/simple/ full-month --trusted-host 192.168.0.1
Looking in indexes: http://192.168.0.1:8000/simple/
User for 192.168.0.1:8000: neozheng
Password: 
Save credentials to keyring [y/N]: N
Collecting full-month
  Downloading http://161.189.163.186:8000/packages/full_month-0.0.4-py3-none-any.whl (3.1 kB)
Installing collected packages: full-month
Successfully installed full-month-0.0.4
MacBook-Pro:aws zhenxink$ 

# 由于 -a 中包含了 dowload, 所以下载安装时也需要输入用户名和密码

下载 pypi server 中没有的包时(如: boto3),也能安装成功,如下所求:

MacBook-Pro:aws zhenxink$ pip install --index-url http://192.168.0.1:8000/simple/ boto3 --trusted-host 192.168.0.1
Looking in indexes: http://192.168.0.1:8000/simple/
User for 192.168.0.1:8000: neozheng
Password: 
Save credentials to keyring [y/N]: N
Collecting boto3
  Downloading https://pypi.doubanio.com/packages/b3/f4/f9ab9845a63cef64d42dea7ef09c593be5ec2b627d398ef7a0954ac0a8f7/boto3-1.16.49-py2.py3-none-any.whl (130 kB)
     |████████████████████████████████| 130 kB 3.3 MB/s 
Collecting botocore<1.20.0,>=1.19.49
  Downloading https://pypi.doubanio.com/packages/15/b9/d4118f2befea084c9bc72aaa9cb10f1cef8311a534cfdc10e02715baa121/botocore-1.19.49-py2.py3-none-any.whl (7.2 MB)
     |████████████████████████████████| 7.2 MB 6.0 MB/s 
Requirement already satisfied: urllib3<1.27,>=1.25.4 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from botocore<1.20.0,>=1.19.49->boto3) (1.25.7)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from botocore<1.20.0,>=1.19.49->boto3) (2.8.1)
Collecting jmespath<1.0.0,>=0.7.1
  Downloading https://pypi.doubanio.com/packages/07/cb/5f001272b6faeb23c1c9e0acc04d48eaaf5c862c17709d20e3469c6e0139/jmespath-0.10.0-py2.py3-none-any.whl (24 kB)
Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.20.0,>=1.19.49->boto3) (1.13.0)
Collecting s3transfer<0.4.0,>=0.3.0
  Downloading https://pypi.doubanio.com/packages/69/79/e6afb3d8b0b4e96cefbdc690f741d7dd24547ff1f94240c997a26fa908d3/s3transfer-0.3.3-py2.py3-none-any.whl (69 kB)
     |████████████████████████████████| 69 kB 8.0 MB/s 
Installing collected packages: jmespath, botocore, s3transfer, boto3
Successfully installed boto3-1.16.49 botocore-1.19.49 jmespath-0.10.0 s3transfer-0.3.3
MacBook-Pro:aws zhenxink$ 

# 此时也需要输入用户名密码

参考链接:

https://www.cnblogs.com/mithrilon/p/8954677.html

https://testdriven.io/blog/private-pypi/

官方文档:

https://pypi.org/project/pypiserver/

原文地址:https://www.cnblogs.com/neozheng/p/14242574.html