在我们的日常生活和工作中,难免会碰到要给别人传文件的时候。可能这对现在的你来说不是一件很难的事情,估计相当多的一部分人说我可以直接把文件拖进微信或者 qq 里发给别人,但这个只适用于文件较少的时候,文件较多的时候用聊天工具来进行文件传输就又变成了一件很麻烦的事情。
这时候你可能又会说,那我可以传到某度网盘上,然后发个链接给对方,这不是美滋滋么?要我说这是苦滋滋吧!你要先把东西传到网盘,然后发链接,最后对方再下载到自己电脑上,要是碰到网速慢的时候中间耗费的时间连黄花菜都凉了,当然你要说你是 vip,那我无话可说了,顺便如果对方也是 vip,建议你现在立马退出本篇文章,因为这不适合家里开矿的大佬。
如果你看过我最近的两篇文章,你一定就会猜到接下来会发生什么了,但凡碰到什么棘手的问题,就是我号称「简洁优雅,省心省力」的 Python 闪亮登场的时候了,知道这是什么么?这就是传说中的主角光环,所以啊,学习 Python 刻不容缓。
Python 中内置了一个「下载服务器」,简单又好用,可以说是老少咸宜,童叟无欺。
操作
Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python。
假设你要给别人传的文件位于某个目录(文件夹)下,首先你要先进入这个目录(文件夹),然后执行下面的命令启动一个下载服务器。例如,我要传的文件在 ~/PycharmProjects/DataScience 目录下:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">cd PycharmProjects/DataScience/
python3 -m http.server
</pre>
如果你用的是 Python2 的话,进入到目标目录下之后,用下面的语句:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">python -m SimpleHTTPServer
</pre>
具体的显示如下图:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558420467312" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
执行了上面的命令,就会在当前的目录(文件夹)下启动一个文件下载服务器,默认打开的是 8000 端口,完成这些以后,你只需要把你的 ip 和 端口号告诉给对方,让对方在浏览器中输入 ip 和 端口号即可下载他想下载的东西,非常简单高效。一般的格式如下:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">xx.xx.xx.xx:8000
</pre>
在浏览器中显示的内容大概如下:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558420467317" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
就如上图所示,你可以看到一个类似于 FTP 下载的页面,这个时候你想下载哪个文件,直接单击即可下载,通过这种方式来传输文件,可以节省很多时间,提高文件传输的效率。
还要再说一点的是,在上面你所看到的 Python 语句,其实就是简单的启动了一个 Python 内置的 web 服务器。如果当前目录(文件夹)下不存在 index.html,就会显示当前目录下的文件列表,也就是我们在上图中看到的内容,如果存在 index.html 的话,会默认显示 index.html 里的内容。
网友评论