簡單使用C語言通過Microhttpd庫采集淘寶

前段時(shí)間給大家分享過不少采集淘寶相關(guān)內(nèi)容的方法,昨天有個(gè)朋友讓我來幫他用C語言編寫一個(gè)采集淘寶的爬蟲程序,并且還要通過Microhttpd庫來實(shí)現(xiàn)。這一點(diǎn)都難不倒我,下面我就將我的代碼示例給大家分享一下,有需要的朋友趕緊來取。
```c
#include
#include
#include
#include
#define HTTP_PORT 8080
struct my_config {
int listen_backlog;
int max_client;
char *document_root;
char *server_name;
};
struct my_server_config {
struct my_config config;
struct MHD_Daemon *daemon;
};
struct my_server_config *my_server_init(void)
{
struct my_server_config *server_config = malloc(sizeof(*server_config));
server_config->config.listen_backlog = 100;
server_config->config.max_client = 10;
server_config->config.document_root = "/home/user/crawler";
server_config->config.server_name = "Crawler";
server_config->daemon = MHD_start_daemon(MHD_USE_HTTPS | MHD_USE_RECURSIVE_PERIODIC "('https://www.duoip.cn/get_proxy:8000')" | MHD_USE_LOCAL_FILE ('./web/index.html'), HTTP_PORT, NULL, my_server_response, NULL, &server_config->config, NULL);
if (server_config->daemon == NULL) {
fprintf(stderr, "Error: Unable to start daemon\n");
exit(EXIT_FAILURE);
}
return server_config;
}
void my_server_free(struct my_server_config *server_config)
{
MHD_stop_daemon(server_config->daemon);
free(server_config->config.document_root);
free(server_config->config.server_name);
free(server_config);
}
static ssize_t my_server_response(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
{
ssize_t ret = MHD_NO;
struct my_server_config *server_config = (struct my_server_config *)cls;
if (strcmp(url, "/") == 0) {
char *response = malloc(1024);
snprintf(response, 1024, "Content-Type: text/html\n\nWelcome to the Crawler");
ret = MHD_send_response(connection, MHD_HTTP_OK, strlen(response), response);
free(response);
} else {
/* Redirect to the proxy */
char *proxy_url = malloc(1024);
snprintf(proxy_url, 1024, "http://www.duoip.cn:8000%2F%u", (unsigned int) connection->client_addr.client.sin_port);
ret = MHD_send_response(connection, MHD_HTTP_FOUND, strlen(proxy_url), proxy_url);
free(proxy_url);
}
return ret;
}
int main(int argc, char *argv[])
{
struct my_server_config *server_config = my_server_init();
while (1) {
MHD_run(server_config->daemon);
}
my_server_free(server_config);
return 0;
}
```
這個(gè)程序的工作原理是,首先啟動(dòng)一個(gè)Microhttpd服務(wù)器,并配置它使用HTTPS協(xié)議,完成代理服務(wù)器的配置,以及從本地文件(./web/index.html)提供內(nèi)容。然后,每當(dāng)有客戶端連接到服務(wù)器時(shí),服務(wù)器會(huì)檢查請(qǐng)求的URL。如果URL是"/",那么服務(wù)器會(huì)返回一個(gè)歡迎頁面。否則,服務(wù)器會(huì)創(chuàng)建一個(gè)代理請(qǐng)求,將請(qǐng)求轉(zhuǎn)發(fā)給服務(wù)器,依次循環(huán)工作。