Python搭建简易HTTP/HTTPS文件服务器 | 您所在的位置:网站首页 › python搭建服务器 › Python搭建简易HTTP/HTTPS文件服务器 |
之前因为传输文件的需要,需要自己搭建文件传输服务器。FTP/FTPS文件服务器比较容易,因为相关的软件很多。关于HTTP/HTTPS文件服务器的介绍相对来说要少很多,网上关于HTTP/HTTPS文件服务器的搭建方法杂乱无章,很多方法根本是不可用的。因此,我特意总结了使用Python搭建简易HTTP/HTTPS文件服务器的方法。 一、从GitHub下载源码经过多次的尝试,我在GitHub发现了一个好用的Python编写的HTTP/HTTPS文件服务器,源码下载地址:https://github.com/eloypgz/httpsweet 。这个文件服务器是用Python3写成的。 二、编译生成文件服务器对源码进行编译: python3 setup.py install生成的最终的可用的文件是:httpsweet-master\build\scripts-3.7\httpsweet。 三、httpsweet的使用方法很遗憾,GitHub的作者并没有详细介绍httpsweet的用法。httpsweet的主要参数: port:监听的端口号,默认为8000 --bind:绑定的IP地址,默认为0.0.0.0 --directory:指定的共享目录 --cert:HTTPS文件服务器需要的证书 --key:HTTPS文件服务器需要的私钥,与--cert参数一起使用 -h:帮助说明 复制代码 1、HTTP文件服务器HTTP文件服务器只需要指定IP地址、端口号和共享目录即可: python3 httpsweet --bind {ip} --directory {directory} {port} 2、HTTPS文件服务器使用HTTPS文件服务器的时候,需要指定证书和私钥。使用openssl生成需要的证书和私钥: openssl genrsa -out test.key 2048 openssl req -new -x509 -days 3650 -key test.key -out test.crt 复制代码其中,test.crt为证书,test.key为私钥。 HTTPS文件服务器的使用方法为: python3 httpsweet --bind {ip} --directory {directory} --cert {cert} --key {key} {port} 四、上传下载文件GitHub上介绍的上传下载文件的方法很繁琐,对于我们来说,简单的curl命令即可实现文件的上传和下载。一般来说,linux系统自带curl命令;windows系统需要单独安装curl,下载地址为:https://curl.se/download.html 。 上传文件命令: curl -o {filename} https://{ip}:{port}/{filename}下载文件命令: curl -T {filename} https://{ip}:{port}/{filename} |
CopyRight 2018-2019 实验室设备网 版权所有 |