简单示例:
python -m SimpleHTTPServer 8080
后面的端口不填,会采用默认端口8000。它会将当前所在的文件夹设置为默认的WebRoot的目录,在浏览器敲入本机地址:
http://localhost:8080
如果当前文件夹有index.html文件,会默认显示该文件;否则,会以文件列表的形式显示目录下所有文件。这样就实现了最基本的文件分享。
官网地址:
SimpleHTTPServer — Simple HTTP request handler
https://docs.python.org/2/library/simplehttpserver.html
原文:
Note
The SimpleHTTPServer
module has been merged into http.server
in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
The SimpleHTTPServer
module defines a single class, SimpleHTTPRequestHandler
, which is interface-compatible with BaseHTTPServer.BaseHTTPRequestHandler
.
The SimpleHTTPServer
module defines the following class:
-
class
-
This class serves files from the current directory and below, directly mapping the directory structure to HTTP requests.
A lot of the work, such as parsing the request, is done by the base class
BaseHTTPServer.BaseHTTPRequestHandler
. This class implements thedo_GET()
anddo_HEAD()
functions.The following are defined as class-level attributes of
SimpleHTTPRequestHandler
:server_version
This will be
"SimpleHTTP/" + __version__
, where__version__
is defined at the module level.-
A dictionary mapping suffixes into MIME types. The default is signified by an empty string, and is considered to be
application/octet-stream
. The mapping is used case-insensitively, and so should contain only lower-cased keys.
extensions_map
The
SimpleHTTPRequestHandler
class defines the following methods:-
This method serves the
'HEAD'
request type: it sends the headers it would send for the equivalentGET
request. See thedo_GET()
method for a more complete explanation of the possible headers.
do_HEAD
( )-
The request is mapped to a local file by interpreting the request as a path relative to the current working directory.
If the request was mapped to a directory, the directory is checked for a file named
index.html
orindex.htm
(in that order). If found, the file’s contents are returned; otherwise a directory listing is generated by calling thelist_directory()
method. This method usesos.listdir()
to scan the directory, and returns a404
error response if thelistdir()
fails.If the request was mapped to a file, it is opened and the contents are returned. Any
IOError
exception in opening the requested file is mapped to a404
,'File not found'
error. Otherwise, the content type is guessed by calling theguess_type()
method, which in turn uses the extensions_map variable.A
'Content-type:'
header with the guessed content type is output, followed by a'Content-Length:'
header with the file’s size and a'Last-Modified:'
header with the file’s modification time.Then follows a blank line signifying the end of the headers, and then the contents of the file are output. If the file’s MIME type starts with
text/
the file is opened in text mode; otherwise binary mode is used.The
test()
function in theSimpleHTTPServer
module is an example which creates a server using theSimpleHTTPRequestHandler
as the Handler.New in version 2.5: The
'Last-Modified'
header.
do_GET
( ) -
SimpleHTTPServer.
SimpleHTTPRequestHandler
(
request,
client_address,
server
)
The SimpleHTTPServer
module can be used in the following manner in order to set up a very basic web server serving files relative to the current directory.
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
The SimpleHTTPServer
module can also be invoked directly using the -m
switch of the interpreter with a port number
argument. Similar to the previous example, this serves the files relative to the current directory.
python -m SimpleHTTPServer 8000
See also
-
Module
- Base class implementation for Web server and request handler.
BaseHTTPServer