#分享一個(gè)很方便的小技巧
一、Python使用SimpleHTTPServer
1.適合環(huán)境,在centos、ubuntu等主流發(fā)行版都自帶Python,在windows下安裝也很容易。
2.切到需要通過HTTP交互的目錄,輸入: python -m SimpleHTTPServer 80 (缺省端口8000)
如果當(dāng)前文件夾有index.html文件,會(huì)默認(rèn)顯示該文件,否則,會(huì)以文件列表的形式顯示目錄下所有文件。
3.腳本方式,由于命令不太好記而且是前臺執(zhí)行,可以使用腳本做成一個(gè)服務(wù)。
#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個(gè)不同密碼不想找的老鐵簡直是福音。
二、Python使用FTP
1.Python已經(jīng)有了,還需要下載FTP的庫。
#pip install pyftpdlib
2.安裝完后,和HTTP服器類似,執(zhí)行以下執(zhí)行:
#python -m pyftpdlib -p21 (端口可選,缺省隨機(jī),默認(rèn)是匿名訪問,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()