Python builds a simple web Server

Source: Internet
Author: User

If you need a simple Web Server, but you don't want to download and install the complex HTTP service programs, such as: Apache,iss. So, Python may help you, using Python to complete a simple built-in HTTP server, so you can put your directories and files are displayed in HTTP, only one thing to do is to install a python.

In fact, this is a very useful way to share files. It is simple to implement a tiny HTTP service program, which requires only one command line under Python. Here's the command line: (assuming we need to share our directory /home/haoel and the IP address is 192.168.1.1)

-M Simplehttpserver

That's fine, and our HTTP service listens on port No. 8000. You will get the following information:

Serving HTTP on 0.0.0.0 Port 8000 ...

You can open your browser (ie or Firefox) and enter the following URL:

http://192.168.1.1:8000

If your directory has a file name named index.html, then this file will become a default page, if there is no such file, then the directory list will be displayed.

If you want to change the port number, you can use the following command:

Python-m simplehttpserver 8080

If you just want this HTTP server to serve your local environment, then you need to customize your Python program, here's an example:

1 ImportSYS2 ImportBasehttpserver3  fromSimplehttpserverImportSimplehttprequesthandler4Handlerclass =Simplehttprequesthandler5ServerClass =Basehttpserver.httpserver6Protocol ="http/1.0"7  8 ifSys.argv[1:]:9Port = Int (sys.argv[1])Ten Else: OnePort = 8000 AServer_address = ('127.0.0.1', Port) -   -Handlerclass.protocol_version =Protocol thehttpd =ServerClass (server_address, Handlerclass) -   -SA =Httpd.socket.getsockname () - Print "serving HTTP on", Sa[0],"Port", Sa[1],"..." +Httpd.serve_forever ()
Python version of FTP

Python does not have a built-in FTP server that can be used directly, so support for third-party components is required, and this component I found is called Pyftpdlib, First installed: Pip install Pyftpdlib

After installation, similar to the HTTP server, execute the following command to start an FTP servers:

Python-m pyftpdlib-p 21

The subsequent 21 ports are still optional, not filled in randomly, and the occupied ports will be skipped. In the browser to address the local computer:

Ftp://localhost:21

At this time, is anonymous access, that is, the user name is anonymous, password is empty, if you want to control access, you need to customize the server, specific can see pyftpdlib Tutorial, I'm here to copy over a paragraph as an introduction:

 fromPyftpdlib.authorizersImportDummyauthorizer fromPyftpdlib.handlersImportFtphandler fromPyftpdlib.serversImportFtpserverdefMain ():#instantiate a dummy authorizer for managing ' virtual ' usersAuthorizer =Dummyauthorizer ()#Define A new user has full r/w permissions and a read-only    #Anonymous UserAuthorizer.add_user ('User','12345','.', perm='ELRADFMWM') authorizer.add_anonymous (OS.GETCWD ())#Instantiate FTP Handler classHandler =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    #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 server class and listen on 0.0.0.0:2121Address = ("', 2121) Server=Ftpserver (address, handler)#set a limit for connectionsServer.max_cons = 256server.max_cons_per_ip= 5#Start FTP ServerServer.serve_forever ()if __name__=='__main__': Main ()

Transferred from: http://coolshell.cn/articles/1480.html

Python builds a simple web Server

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.