我们有很多种方式上传文件到服务器上,比如FTP
, 比如使用命令行工具:SZ/RZ
, SCP
等等的。
我之前经常使用的是SZ/RZ
这个命令,但是在Mac下经常乱码然后卡住不动了,所以我很崩溃。下面说说我现在
采用的办法吧。
python -m SimpleHTTPServer 9999
文件名:SimpleHTTPServerWithUpload.py
#!/usr/bin/env python
"""Simple HTTP Server With Upload.
This module builds on BaseHTTPServer by implementing the standard GET
and HEAD requests in a fairly straightforward manner.
"""
__version__ = "0.1"
__all__ = ["SimpleHTTPRequestHandler"]
__author__ = "bones7456"
__home_page__ = "http://li2z.cn/"
import os
import posixpath
import BaseHTTPServer
import urllib
import cgi
import shutil
import mimetypes
import re
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""Simple HTTP request handler with GET/HEAD/POST commands.
This serves files from the current directory and any of its
subdirectories. The MIME type for files is determined by
calling the .guess_type() method. And can reveive file uploaded
by client.
The GET/HEAD/POST requests are identical except that the HEAD
request omits the actual contents of the file.
"""
server_version = "SimpleHTTPWithUpload/" + __version__
def do_GET(self):
"""Serve a GET request."""
f = self.send_head()
if f:
self.copyfile(f, self.wfile)
f.close()
def do_HEAD(self):
"""Serve a HEAD request."""
f = self.send_head()
if f:
f.close()
def do_POST(self):
"""Serve a POST request."""
r, info = self.deal_post_data()
print r, info, "by: ", self.client_address
f = StringIO()
f.write('3.2 Final//EN">')
f.write("\nUpload Result Page \n")
f.write("<body>\nUpload Result Page
\n")
f.write("
\n")
if r:
f.write("Success:")
else:
f.write("Failed:")
f.write(info)
f.write("
"%s\">back" % self.headers['referer'])
f.write("
Powerd By: bones7456, check new version at ")
f.write("")
f.write("here.\n