首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    python实用小工具介绍

    一、秒级启动一个HTTP下载服务器 在实际工作中,时不时会有这样的一个需求:将文件传给其他同事。将文件传给同事本身并不是一个很繁琐的工作,现在的聊天工具一般都支持文件传输。但是,如果需要传送的文件较多,那么,操作起来就会比较麻烦。此外,如果文件在远程的服务器上,你要将文件传给同事,则需要先将远程服务器的文件下载到本地,然后再通过聊天工具传给同事。再或者,你并不是特别清楚要传哪几个文件给同事,所以,你们需要进行来回的交流。交流的时间成本是比较高的,会降低办事效率。此时,你们需要更加高效的方法。这个时候,如果你知道Python内置了一个下载服务器就能够显著提升效率了。例如,你的同事要让你传的文件位于某一个目录下,那么,你可以进入这个目录,然后执行下面的命令启动一个下载服务器: 本地有个一文件夹,想共享给局域网同事下载一些里面的文件,可以使用python的如下命令。 • python2的用法如下: python -m SimpleHTTPServer • python3的用法如下: python3 -m http.server --cgi 以上两种方法默认端口8000,可以制定端口,例如指定端口45678: python -m SimpleHTTPServer 45678 python3 -m http.server --cgi 45678

    02

    Python和sendfile[通俗易懂]

    sendfile(2) is a UNIX system call which provides a “zero-copy” way of copying data from one file descriptor (a file) to another (a socket). Because this copying is done entirely within the kernel, sendfile(2) is more efficient than the combination of “file.read()” and “socket.send()”, which requires transferring data to and from user space. This copying of the data twice imposes some performance and resource penalties which sendfile(2) syscall avoids; it also results in a single system call (and thus only one context switch), rather than the series of read(2) / write(2) system calls (each system call requiring a context switch) used internally for the data copying. A more exhaustive explanation of how sendfile(2) works is available here, but long story short is that sending a file with sendfile() is usually twice as fast than using plain socket.send(). Typical applications which can benefit from using sendfile() are FTP and HTTP servers.

    01
    领券