Using python to achieve http and ftp services for data transmission methods
- 2021-01-06 00:40:06
- OfStack
http data transfer between servers
Use http services built into python directly:
python -m SimpleHTTPServer 8000
At this time, the directory of the input instruction has already enabled http service. 8000 is the port (if not specified, the default is 8000). If we need to fetch files from this directory on other machines, we only need to run it on the target machine:
wget :port/ filename
Speed lever.
Open ftp to upload files
Install the third side component of python for ftp
pip install pyftpdlib
Write startup scripts
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import os
def main():
# Instantiate a dummy authorizer for managing 'virtual' users
authorizer = DummyAuthorizer()
# Define a new user having 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 server class and listen on 0.0.0.0:2121
address = ('', 8888)
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()
8888 is the port number I set, user is the user name, and 12345 is the password I specified. At this time, we need to run the script, we can use the ftp tool, connect to the ftp server, and upload the file.
If we do not use our own script, but directly use the built-in script:
python -m pyftpdlib -p 8888
At this time, connect to the ftp server, using the default user: anonymous, but when we upload the file, we will find that the user does not have upload permission, so it is recommended to write your own running script.