Improved one-line Python for file sharing-supports concurrency

Source: Internet
Author: User

Simplehttpserver is a simple static HTTP server that comes with Python, so you can open the HTTP server by simply switching to the directory you want to share and entering the following command python2.x:

Python-m Simplehttpserver [Port]

Where port is an optional parameter that specifies the ports to use, with port 8000 being used by default.

In python3.x, the Simplehttpserver module is incorporated into the Http.server module, so the commands in the python3.x are slightly different:

Python3-m Http.server [Port]

After you open the server, simply enter the server Ip:port in any LAN device browser to view and download the files for the server-specified directory. As shown in the following:

Figure 1. Browser-displayed file directory

Although this file server is simple, it can make full use of the bandwidth of the LAN, for companies, homes and schools and other scenes to save the Memory stick copy files.

Of course, because of the simple it also has a lot of flaws, it is important that it does not support concurrency, that is, you cannot have multiple hosts to access the shared directory. To solve this problem, rewrite this module:

python2.x:

#!/usr/bin python#Encoding:utf-8ImportSocketImportBasehttpserver fromSimplehttpserverImportSimplehttprequesthandler as SHRH fromSocketserverImportForkingtcpserver as FTSclassForkinghttpserver (FTS):PassdefMain (serverhandlers = SHRH, ServerClass =forkinghttpserver): Basehttpserver.test (Serverhandlers, ServerClass)if __name__=='__main__':    Print("host_address:[%s]"%Socket.gethostbyname (Socket.gethostname ())) main ()

python3.x

#!/usr/local/bin Python3#Encoding:utf-8ImportSocket fromSocketserverImportForkingtcpserver as FTSImportHttp.server fromHttp.serverImportSimplehttprequesthandler as SHRHclassForkhttpserver (FTS):PassdefTest (SERVERHANDLER=SHRH, serverclass=forkhttpserver): Http.server.test (Serverhandler, ServerClass)if __name__=='__main__': IP_Address=Socket.gethostbyname (Socket.gethostname ())Print("Server address:[%s]"%ip_address) test ()

In the code above, ServerClass inherits from the Forkingtcpserver class, which is the synthesis of forkingmixin and TCPServer in the Socketserver module, which realizes the concurrency of the HTTP server by using the branching method.

Save the code as forkhttpserver.py and move it to the Python library directory (my python2.7 is/usr/lib/python2.7;python3.4 to /library/frameworks/ Python.framework/versions/3.4/lib), use the following command to launch a concurrent version of the simple static file server:)

[python2.7]

Python-m Forkhttpserver [Port]

[python3.4]

Python3-m Forkhttpserver [Port]

This command is still too cumbersome for Linux or Mac users to take a step further and alias the command:

Vim ~/.BASHRC

Add to:

[python2.7]

Alias share="python-m/usr/lib/python2.7/forkstaticserver"

[python3.4]

Alias share="python3-m/library/frameworks/python.framework/versions/3.4/lib/forkstaticserver "

This way, as long as the "share" command in the terminal can be opened to the file server, it is convenient, right:-D

Improved one-line Python for file sharing-supports concurrency

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.