#分享一个很方便的小技巧
一、Python使用SimpleHTTPServer
1.适合环境,在centos、ubuntu等主流发行版都自带Python,在windows下安装也很容易。
2.切到需要通过HTTP交互的目录,输入: python -m SimpleHTTPServer 80 (缺省端口8000)
如果当前文件夹有index.html文件,会默认显示该文件,否则,会以文件列表的形式显示目录下所有文件。
3.脚本方式,由于命令不太好记而且是前台执行,可以使用脚本做成一个服务。
#vi httpserver.py
importSimpleHTTPServer
importSocketServer
PORT =8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print"serving at port", PORT
httpd.serve_forever()
#python httpserver.py 或 nohup python httpserver.py > /tmp/httpserver.log 2>&1 &
4.在linux下也可以用links ip:端口访问,可以通过http传输文件,对于有n个不同密码不想找的老铁简直是福音。
二、Python使用FTP
1.Python已经有了,还需要下载FTP的库。
#pip install pyftpdlib
2.安装完后,和HTTP服器类似,执行以下执行:
#python -m pyftpdlib -p21 (端口可选,缺省随机,默认是匿名访问,anonymous,密码为空)
3.如果需要自定义ftp策略,则需要修改源码。
源码地址https://github.com/giampaolo/pyftpdlib
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
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 = ('', 2121)
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()