首页 > 其他分享 >简单使用C语言通过Microhttpd库采集淘宝

简单使用C语言通过Microhttpd库采集淘宝

时间:2023-11-02 16:36:42浏览次数:31  
标签:struct config MHD server char Microhttpd 淘宝 C语言 my

简单使用C语言通过Microhttpd库采集淘宝_#include

前段时间给大家分享过不少采集淘宝相关内容的方法,昨天有个朋友让我来帮他用C语言编写一个采集淘宝的爬虫程序,并且还要通过Microhttpd库来实现。这一点都难不倒我,下面我就将我的代码示例给大家分享一下,有需要的朋友赶紧来取。

```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;
}
```

这个程序的工作原理是,首先启动一个Microhttpd服务器,并配置它使用HTTPS协议,完成代理服务器的配置,以及从本地文件(./web/index.html)提供内容。然后,每当有客户端连接到服务器时,服务器会检查请求的URL。如果URL是"/",那么服务器会返回一个欢迎页面。否则,服务器会创建一个代理请求,将请求转发给服务器,依次循环工作。

标签:struct,config,MHD,server,char,Microhttpd,淘宝,C语言,my
From: https://blog.51cto.com/u_14448891/8150001

相关文章

  • C语言宏的使用
    1、#pragmapack给编译器用的参数设置,有关结构体字节对齐方式设置,#pragmapack是指定数据在内存中的对齐方式。#pragmapack(n)C编译器将按照n个字节对齐。#pragmapack()取消自定义字节对齐方式。#pragmapack(push,1)把原来对齐方式设置压栈,......
  • C语言中静态函数和非静态函数的区别
     C语言中,静态函数和非静态函数的区别主要体现在以下方面:可见性:静态函数只能在当前文件内使用,不能被其他文件调用,而非静态函数可以被当前文件以外的其他文件调用,具有全局可见性。生命周期:静态函数在程序运行期间一直存在,不会被释放,而非静态函数则是在被......
  • C语言经典练习题1
    1、题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第了个人大2岁,问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数......
  • 实验3 C语言函数应用编程
    任务1源码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_bl......
  • 三道同学的oj遇到的问题,C语言
    1.请写一段程序来判断表达式是否相等。输入格式:只有一行,为三个用空格分隔的浮点数a,b,c(0<a,b,c<100)。输出格式:也只有一行,如果a-b等于c,则输出yes,否则输出no。1//原答案,未满分2intmain(void){3doublea,b,c;45scanf("%f%f%f",&a,&b,&c);......
  • C语言经典例题7加解析
    1、输入长方形的长和寬,编程求该长方形的周长和面积#include<stdio.h>intmain()(floatlength,width;floatperimeter,area;//输入长方形的长和宽printf(“请输入长方形的长:“);scant(“%r”,&length);printf(”请输入长方形的宽:“);scanf(“%r”,&width);//计算周长和面......
  • c语言学习(结构体初始化)42
    利用例题直接阐释:#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>structhobby//hobby:爱好{charcolor[10];charsports[10];};structstu{charname[10];intage;structhobbys2;doubleheight;};intmain(){structstu......
  • 实验3_c语言函数应用编程
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,col......
  • 《安富莱嵌入式周报》第320期:键盘敲击声解码, 军工级boot设计,开源CNC运动控制器,C语言
     视频版:https://www.bilibili.com/video/BV1Cr4y1d7Mp/1、键盘敲击声解码https://arxiv.org/abs/2308.01074键盘敲击声被解码的话,我们使用键盘输入密码将被方便的解码出来。这篇文章介绍了一种使用最先进的深度学习模型,以便使用手机麦克风对笔记本电脑敲击键盘分析。实际测试训练......
  • C语言-2.结构学习
    结构1.1顺序~2.1选择~3.1.1关系运算符类型类型优先级小于<小于等于<=优先级相同(高)大于>大于等于>=优先级相同(高)等于==不等于!=优先级相同(低)tips:将数值放前,可以判断该字符是否为前面已经运用的变量。如3=a,若前面a已经被赋值,则会出现语法错......