Create a simple HTTP and FTP service using Python

Source: Internet
Author: User

Python version http Server

Little gossip, first make sure that pythonis installed, I have the 2.x version, yes, my operating system is WIN7, in fact, for Python , what operating system is not important. Python has a simple HTTP server built-in, with just one command line underneath it, and an HTTP server up:

Python-m Simplehttpserver 80

The following 80 ports are optional and do not fill with the default port of 8000. 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 a index.html file, the file is displayed by default, otherwise all files under the directory are displayed as a list of files. This has achieved the most basic purpose of file sharing, you can make a script, and then create a shortcut, you can easily start sharing the file. If there are more needs, can be customized according to their own needs, specific see the Official document Simplehttpserver, or directly read the source code. I copy a paragraph for easy reference:

Import Simplehttpserver Import  = 8000== Socketserver.tcpserver (("", PORT), Handler)print " Serving at Port " , Porthttpd.serve_forever ()

Python version ftp server

See here, by default you already have Pythoninstalled, but you also need to install another handy tool. You know, when you need to find the Chrome plugin, will go to Google 's WebStore, when need to find Firefox app, will go to Mozilla 's Add-ons: When you need to find a Python component, you need pip:a tool for installing and managing Python packages, The installation method is not introduced.

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

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 ()

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

Transferred from: https://www.cnblogs.com/yili16438/p/d3209323913c6d53e6060fcd8d27e4c0.html

Create a simple HTTP and FTP service using Python

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.