Use Python to create simple HTTP services (based on simplehttpserver) and FTP services (based on Pyftpdlib)

Source: Internet
Author: User
Tags anonymous

reproduced from: http://www.cnblogs.com/yili16438/p/d3209323913c6d53e6060fcd8d27e4c0.html


One uses Python's built-in simplehttpserver to create HTTP services

(1) Windows CMD Mode (assuming that the computer has been installed in advance python)

Under Start/Run/cmd, switch to a directory with the CD command, and then typing:

Python-m Simplehttpserver 80  

the 80 ports that follow are optional and the default port 8000 is not filled in. Note that this will set the current folder as the default Web directory, and try typing the native address in the browser:

http://localhost:80

If the current folder has index.html files, the file will be displayed by default, or all files in the directory will be displayed as a list of files. Run over, please CTRL + C, and close cmd.

(2) Scripting Way

Can be made into a script such as test.py, and then create a shortcut, you can easily start to share the file. If there is more demand, can be customized according to their own needs, specific see the Official document Simplehttpserver, or directly look at the source code. I copy a paragraph for easy reference:

Import simplehttpserver
import socketserver
PORT = 8000
Handler = Simplehttpserver.simplehttprequesthandler
httpd = Socketserver.tcpserver ((", PORT), Handler)
print" Serving at Port ", Port
Httpd.serve_forever ()

Try typing the native address in the browser:

http://localhost:8000

If there is a index.html file in the current folder where test.py resides, the file is displayed by default, or all files in the directory are displayed as a list of files. Run over, please CTRL + C, and close cmd.

Second Python version of the FTP server

See here, you've installed Pythonby default, but you'll need to install another handy tool. You know, when you need to find the Chrome plugin, will go to Google 's webstore, when you need to find Firefox applications, will go to Mozilla 's add-onsWhen you need to find the Python component, you need PIP:A tool for installing and managing Python packages, installation method See (first install PIP, Then set the system variable, then install the Pyftpdlib module with the pip install Pyftpdlib command, and remember to install the "C:\Python27\Scripts" to the system path after installing the PIP, otherwise in cmd, type "Pip" List "will not respond oh. Explore yourself slowly, this is not detailed here.

Python does not have a directly available FTP server, so it requires Third-party component support, and I found this component called Pyftpdlib, first installed:

Pip Install Pyftpdlib

After installation, similar to the HTTP service, you can start an FTP server by executing the following command:

Python-m pyftpdlib-p 21

The rear 21 ports are still optional and will not fill the random one, and the occupied ports will skip. Typing the native address in the browser:

Ftp://localhost:21

This time, is anonymous access, that is, the user name is anonymous , the password is empty, if you want to control access, you need to customize the server, you can refer to Pyftpdlib Tutorial, I'm copying it here. For an introduction:

From pyftpdlib.authorizers import Dummyauthorizer to pyftpdlib.handlers import Ftphandler from Pyftpdlib.servers Import Ftpserver def main (): # Instantiate a dummy authorizer for managing ' virtual ' users authorizer = Dummyauth Orizer () # Define A new user has full r/w permissions and a read-only # anonymous user Authorizer.add_user
    (' User ', ' 12345 ', '. ', perm= ' ELRADFMWM ') authorizer.add_anonymous (OS.GETCWD ()) # Instantiate FTP Handler class Handler = Ftphandler Handler.authorizer = authorizer # Define a customized banner (string returned when client

    Connects) Handler.banner = "Pyftpdlib based ftpd ready."  # Specify a masquerade address and the range of ports to use for # passive connections.
    Decomment in case you ' re behind a NAT. #handler. masquerade_address = ' 151.25.42.11 ' #handler. Passive_ports = Range (60000, 65535) # Instantiate FTP serve R class and listen on 0.0.0.0:2121 address = (', 2121 ') server =Ftpserver (address, handler) # Set a limit for connections server.max_cons = 256 SERVER.MAX_CONS_PER_IP = 5 # Start FTP Server server.serve_forever () if __name__ = = ' __main__ ': Main ()

Just look at the code should basically know how to use,Add_user is obviously to add users,2121 is a designated port, of course, can also be random, and the maximum number of connections max_cons, each IP maximum connection limit, More interfaces suggest a direct look at docstrings.

===============================================================================

2015.8.29 Update

The above code I did not use up, looked at Git hub, after testing, such as the following code most practical, you can make a name for example ftpserver.py file, can be used later, save to download other FTP software, one installation too troublesome, and set too troublesome. The way of landing is suggested to adopt admin/123456 way, do not suggest anonymous way, anyway anonymous way I did not succeed.

From pyftpdlib.authorizers import Dummyauthorizer
From pyftpdlib.handlers import Ftphandler
From pyftpdlib.servers import Ftpserver

Authorizer = Dummyauthorizer ()
# # Add user name/password/ftp directory. When the directory uses the "." Represents the directory where the. py file is currently located
Authorizer.add_user ("admin", "123456", "E:/download", perm= "ELRADFMWM")
# # FTP directory when adding anonymous access
Authorizer.add_anonymous ("E:/download", perm= "ELRADFMWM")
Handler = Ftphandler
Handler.authorizer = Authorizer

# # Set the FTP local or IP address/port, you can modify the IP and port according to their actual situation.
# server = Ftpserver ("0.0.0.0", Handler)
# server = Ftpserver ("127.0.0.1", Handler)
Server = Ftpserver ("192.168.1.N", Handler)
Server.serve_forever ()


or use the following method that does not use the per-machine IP

From pyftpdlib.authorizers import Dummyauthorizer
From pyftpdlib.handlers import Ftphandler
From pyftpdlib.servers import Ftpserver
Import socket

# automatically obtain native IP address
ip = socket.gethostbyname (socket.gethostname ())
Authorizer = Dummyauthorizer ()
# # Add user name/password/ftp directory. When the directory uses the "." Represents the directory where the. py file is currently located
Authorizer.add_user ("admin", "123456", "E:/download", perm= "ELRADFMWM")
# # FTP directory when adding anonymous access
Authorizer.add_anonymous ("E:/download", perm= "ELRADFMWM")
Handler = Ftphandler
Handler.authorizer = Authorizer

# # Set the FTP local or IP address/port, you can modify the IP and port according to their actual situation.
# server = Ftpserver ("0.0.0.0", Handler)
# server = Ftpserver ("127.0.0.1", Handler)
Server = Ftpserver (IP, handler)
Server.serve_forever ()

============================================
PostScript

Python 's third-party component is a huge treasure trove, and most of the problems I've encountered can be found in this area. At the same time, it is recommended that programmers who like to toss around, even those who do not have a program background, will try to learn the language, which is of great benefit to the ability to solve problems and exercise of thinking.


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.