简单示例:
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:
SimpleHTTPServer.
SimpleHTTPRequestHandler
(request, client_address, server)
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 the do_GET()
and do_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.
extensions_map
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.
The SimpleHTTPRequestHandler
class defines the following methods:
do_HEAD
()
This method serves the 'HEAD'
request type: it sends the headers it would send for the equivalent GET
request. See the do_GET()
method for a more complete explanation of the possible headers.
do_GET
()
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
or index.htm
(in that order). If found, the file’s contents are returned; otherwise a directory listing is generated by calling the list_directory()
method. This method uses os.listdir()
to scan the directory, and returns a 404
error response if the listdir()
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 a 404
, 'File not found'
error. Otherwise, the content type is guessed by calling the guess_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 the SimpleHTTPServer
module is an example which creates a server using the SimpleHTTPRequestHandler
as the Handler.
New in version 2.5: The 'Last-Modified'
header.
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
相关推荐
本文档基于 Python 2.x 版本进行介绍,但大部分内容也适用于 Python 3.x 版本(需要注意的是,在 Python 3.x 中,`SimpleHTTPServer` 已经被整合到 `http.server` 模块中)。 #### 三、启动服务器 在命令行中输入...
标题中的“使用python SimpleHTTPServer 快速搭建Web服务器”是指利用Python内置的SimpleHTTPServer模块来创建一个简单的HTTP服务器,这个服务器可以用于本地文件共享、测试网页或者演示静态内容。SimpleHTTPServer...
一个可用于局域网上传下载的 Python 模块,使用介绍查看该文章:使用Python开启局域网传送数据(3):使用 simple_http_server 实现上传下载功能(https://blog.csdn.net/qq_45476428/article/details/129721441)。...
【标题】cpp-SimpleHTTPServer采用Rust开发的简单HTTP服务器 在当今互联网技术日新月异的时代,HTTP服务器作为Web应用的基础,扮演着至关重要的角色。本文将深入探讨一个用Rust编程语言实现的简单HTTP服务器——...
《构建简易HTTP服务器:基于C#的SimpleHttpServer解析》 在信息技术领域,HTTP服务器是互联网基础设施的重要组成部分,它负责接收HTTP请求并返回相应的HTTP响应。本文将详细讲解如何使用C#语言创建一个简单的HTTP...
总结,"SimpleHttpServer.zip"项目是一个基于C#实现的简易HTTP服务器,涵盖了HTTP请求处理、HTTP响应构建和可能的WebSocket支持。通过这个项目,我们可以学习到如何在C#中构建网络服务,理解HTTP协议的工作原理,...
简单的http 一个非常简单的网络服务器。 它仅提供静态内容并由 CLI 选项配置。 关于: 有时我想要一个简单的 http 服务器,它不需要很多... usage: simplehttp [-h] [-i INTERFACE] [-p PORT] [-r ROOT] optional a
simple-httpd替代Python SimpleHTTPServer的简单HTTP服务器,并提供自动TLS通过Let's Encrypt over HTTP2实现。支持HTTP GET和HEAD请求,并遵守HTTP / 1.1 RFC 2616准则。
总的来说,"SimpleHttpServer.rar"中的示例展示了如何利用C#的HttpListener类创建一个基础的HTTP服务器,具备处理高并发请求的能力。这不仅对于学习网络编程和服务器开发有极大的帮助,也是理解Web服务器工作原理的...
simple_http_server 产品特点 :check_mark: 简单 :check_mark: 上载 :check_mark: 下载 用法 # get code $ git clone https://github.com/freelamb/simple_http_server.git # enter directory $ cd simple_...
搭建FTP,或者是搭建网络文件系统,这些方法都能够实现Linux的目录共享。但是FTP和网络文件系统的功能都过于强大,因此它们都有一些不够方便的地方。比如你想快速共享Linux系统的...它在Python 3已经合并到http.server
simpleHTTPServer 节点的非常简单的HTTP Server配置。 我的意思是非常简单的部分。用法: 只需添加类似 "simpleHttpServer": "git://github.com/AntonioMA/simpleHTTPServer",到您的package.json devDependencies...
开源项目-briandowns-simple-httpd.zip,Simple HTTP server replacement for Python SimpleHTTPServer. Optional TLS via Let's Encrypt w/HTTP2
simplehttpserver:简单的HTTP服务器 'simpehttpserver'是python的SimpleHTTPServer的简单模仿,旨在用于测试,开发和调试目的 全局安装 使用npm安装 npm install simplehttpserver -g 用法 通过命令运行...
简单的 CORS 服务器安装npm install simple-cors-server -g用法cd 到你的项目路径simplecors 您还可以选择指定要运行的端口(如果出现 EACCES 错误,则需要指定此端口,这意味着您有另一台服务器在该端口上运行): ...
SimpleHttpServer 用C编写的非常简单和粗略的HTTP服务器 特征 支持静态页面的GET请求 通过使用FastCGI支持对PHP页面的GET / POST请求 Linux上的多处理模式 建造 在这两个平台上,默认情况下,编译后的可执行文件...
SimpleHTTPserver是众所周知的python simplehttpserver的增强版本,此外还具有完全可自定义的TCP服务器,均支持TLS。 特征 HTTPS支持 任意目录中的文件服务器 完整的请求/响应转储 可配置的IP地址和监听端口 通过...
简单HTTP服务器 Java中的简单HTTP服务器 :globe_with_meridians: :open_...克隆项目: git clone https://github.com/MalakSadek/Simple-HTTP-Server 将CD放入目录并编译文件,然后运行源代码: cd Java-Web-Server-