SoFunction
Updated on 2024-11-12

A Quick Way to Implement HTTP and FTP Servers with Python

Sometimes you need to improvise a simpleWeb ServerBut you don't want to install it.ApacheNginx and other such more complex functionsHTTP service program. This can be done using thePython built-inSimpleHTTPServer module to quickly build a simpleHTTP Server.

SimpleHTTPServer module takes the files and folders in the directory you specify in a simpleWeb The way the page is presented. Let's say we need to display a page in the form of aWeb Way Shared Directory/Users/Mike/Docker, which can be easily accomplished with just this command line below:

$ cd /Users/Mike/Docker
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

SimpleHTTPServer By default, the module listens on port 8000 for aHTTP service, you can then open your browser and typehttp://IP:Port Visit thisWeb Page. For example, a URL like the one below:

http://192.168.100.49:8000

If you needWeb The service has a default page that creates a file named in the directory. If there is no default page, then the contents of the directory are displayed as a list.

If the default port 8000 is already occupied and you want to switch to using another port number, you can use the following command:

$ python -m SimpleHTTPServer 8080

Quick FTP Server Implementation in Python

Sometimes when you want to quickly build aFTP This is particularly useful when the server is used to temporarily implement file uploads and downloads. Here we utilize thePython (used form a nominal expression)Pyftpdlib module can quickly implement aFTP Server functions.

first installPyftpdlib module (in software)

$ sudo pip install pyftpdlib

pass (a bill or inspection etc)Python (used form a nominal expression)-m The option willPyftpdlib module to run as a simple standalone server, assuming we need the shared directory/Users/Mike/Docker, which can be easily accomplished with just this command line below:

$ cd /Users/Mike/Docker
$ python -m pyftpdlib
[I 2018-01-02 16:24:02] >>> starting FTP server on :::2121, pid=7517 <<<
[I 2018-01-02 16:24:02] concurrency model: async
[I 2018-01-02 16:24:02] masquerade (NAT) address: None
[I 2018-01-02 16:24:02] passive ports: None

So far a simpleFTP The server has been set up, visitftp://IP:PORT This will work. For example, a URL like the one below:

ftp://192.168.100.49:2121

  • The default IP is all available IPs on the machine and port 2121.
  • The default login is anonymous.
  • The default permissions are read-only

If you want to build an authenticated and writableFTP server, a command similar to the following can be used:

$ python -m pyftpdlib -i 192.168.100.49 -w -d /tmp/ -u mike -P 123456

Trivia: Always use a password when testing000000 Such a weak password for the authentication password keeps prompting authentication failure when logging in on the client. It seems thatPyftpdlib The module also does basic security policies yo, nice one!

Description of commonly used optional parameters.

-i indicate clearly and with certaintyIPaddress(Defaults to all locally available IP address)
-p indicate clearly and with certainty端口(default 2121)
-w write access(default只读)
-d indicate clearly and with certainty目录 (default当前目录)
-u indicate clearly and with certainty登录用户名
-P indicate clearly and with certainty登录密码

More parameters can be queried using the following commands:

$ python -m pyftpdlib --help
Usage: python -m pyftpdlib [options]
Start a stand alone anonymous FTP server.
Options:
 -h, --help
 show this help message and exit
 -i ADDRESS, --interface=ADDRESS
 specify the interface to run on (default all interfaces)
 -p PORT, --port=PORT
 specify port number to run on (default 2121)
 -w, --write
 grants write access for logged in user (default read-only)
 -d FOLDER, --directory=FOLDER
 specify the directory to share (default current directory)
 -n ADDRESS, --nat-address=ADDRESS
 the NAT address to use for passive connections
 -r FROM-TO, --range=FROM-TO
 the range of TCP ports to use for passive connections (. -r 8000-9000)
 -D, --debug
 enable DEBUG logging evel
 -v, --version
 print pyftpdlib version and exit
 -V, --verbose
 activate a more verbose logging
 -u USERNAME, --username=USERNAME
 specify username to login with (anonymous login will be disabled and password required if supplied)
 -P PASSWORD, --password=PASSWORD
 specify a password to login with (username required to be useful)

If you need to uninstallPyftpdlib module, you can use the following command:

$ pip uninstall pyftpdlib

reference document

https:///article/

/en/latest/

https:///article/

summarize

The above is a small introduction to the use of Python quickly realize the HTTP and FTP server method, I hope to help you, if you have any questions please leave me a message, I will promptly reply to everyone. I would also like to thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!