使用python实现http及ftp服务进行数据传输的方法

yipeiwu_com6年前Python基础

服务器之间的http数据传输

直接使用python内置的http服务:

python -m SimpleHTTPServer 8000

此时,输入指令的目录就已经开启了http服务,8000为端口(如不指定,默认为8000),如果我们需要在其他机器下垃取该目录下的文件,只需在目标机器运行:

wget ip:port/文件名

速度杠杆的。

开启ftp上传文件

安装ftp的python第三方组件

pip install pyftpdlib

编写启动脚本

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是我设定的端口号,user是用户名,12345是我指定的密码,此时,我们至需要运行脚本,就可以使用ftp工具,连接该ftp服务器,并上传文件了。

如果我们不使用我们自己编写的脚本,而是直接使用内置的脚本:

python -m pyftpdlib -p 8888

此时,连接该ftp服务器,使用的是默认的用户:anonymous,但是当我们上传文件时,会发现,没有该用户的上传权限,所以,这里建议自己编写运行脚本。

以上这篇使用python实现http及ftp服务进行数据传输的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 函数的缺省参数使用注意事项分析

本文实例讲述了python 函数的缺省参数使用注意事项。分享给大家供大家参考,具体如下: python的函数支持4种形式的参数:分别是必选参数、 缺省参数、 可变长参数、关键字参数;而且...

使用Python3中的gettext模块翻译Python源码以支持多语言

使用Python3中的gettext模块翻译Python源码以支持多语言

你写了一个Python 3程序,还想要它适用于其他语言。你能复制全部代码库,然后刻意地检查每个.py文件,替换掉所有找到的文本字符串。但这意味着你有两份你代码的独立副本,每当你要做出个改...

PyQt6在全新电脑怎么样安装

在全新的电脑上安装 PyQt6,你需要先确保你的电脑上已安装了 Python 和 pip(Python 的包管理器)。以下是详细的步骤指导: 1. 安装 Python访问 Python 官方网站 下...

pytorch实现线性拟合方式

pytorch实现线性拟合方式

一维线性拟合 数据为y=4x+5加上噪音 结果: import numpy as np from mpl_toolkits.mplot3d import Axes3D from m...

python中的五种异常处理机制介绍

从几年前开始学习编程直到现在,一直对程序中的异常处理怀有恐惧和排斥心理。之所以这样,是因为不了解。这次攻python,首先把自己最畏惧和最不熟悉的几块内容列出来,里面就有「异常处理」这一...