2019独角兽企业重金招聘Python工程师标准>>>
Mongoose Web Server是一款易于使用的Web服务器,它可以嵌入到其它应用程序中,为其提供Web接口。
主要特写:
-
跨平台,支持 Windows、OS X 和 Linux
-
支持 CGI, SSL, SSI, Digest (MD5) 认证,WebSocket 和 WebDAV
-
支持断点续传和 URL 重写
-
基于 IP 的 ACL,支持 Windows 服务,支持 GET, POST, HEAD, PUT, DELETE 方法
-
Excluding files from serving by URI pattern
-
下载速度限制,基于客户端子网和 URI 模式
-
体积小,可执行文件只有 40k 左右
-
可嵌入式,提供简单的 API (mongoose.h). 只需一个源码文件 mongoose.c
-
嵌入式实例: hello.c, post.c, upload.c, websocket.c
-
提供 Python 和 C# 版本
-
采用 MIT 授权协议
doc: https://docs.cesanta.com/mongoose/dev/
code : https://github.com/cesanta/mongoose
一个简单的http服务器:
// Copyright (c) 2015 Cesanta Software Limited
// All rights reserved
#include "mongoose.h"
static const char *s_http_port = "8000";
static struct mg_serve_http_opts s_http_server_opts;
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
if (ev == MG_EV_HTTP_REQUEST) {
mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
}
}
int main(void) {
struct mg_mgr mgr;
struct mg_connection *nc;
mg_mgr_init(&mgr, NULL);
printf("Starting web server on port %s\n", s_http_port);
nc = mg_bind(&mgr, s_http_port, ev_handler);
if (nc == NULL) {
printf("Failed to create listener\n");
return 1;
}
// Set up HTTP server parameters
mg_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = "."; // Serve current directory
s_http_server_opts.enable_directory_listing = "yes";
for (;;) {
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
Copy mongoose.c and mongoose.h to your build tree
Write code that uses Mongoose API, e.g. in my_app.c
Compile application: $ cc simplest_web_server.c mongoose.c -o simplest_web_server.exe