下面来给各位介绍一段用php实现的Libevent HTTP客户端实现程序,有需要了解的朋友可与小编一起来学习一下.
php Libevent HTTP,代码如下:
<?php //请求完成回调 function _request_handler($req, $base) { global $pend_req; //echo __FUNCTION__, PHP_EOL; if (is_null($req)) { //echo "Timed out\n"; } else { $response_code = $req->getResponseCode(); if ($response_code == 0) { //echo "Connection refused\n"; } elseif ($response_code != 200) { //echo "Unexpected response: $response_code\n"; } else { //echo "Success: $response_code\n"; /* $buf = $req->getInputBuffer(); echo "Body:\n"; while ($s = $buf->readLine(EventBuffer::EOL_ANY)) { echo $s, PHP_EOL; } */ } } $pend_req--; //退出循环 if (!$pend_req) { $base = $conn->getBase(); $base->exit(NULL); } //释放内存 unset($req); unset($conn); } //$address = "www.phprm.com"; $pend_req = 0; $port = 80; //初始化event base $base = new EventBase(); echo "Event method used: ", $base->getMethod(), PHP_EOL; //使用异步DNS $dns_base = new EventDnsBase($base, TRUE); $f= fopen("./50000.txt","r"); while (!feof($f)) { $line = fgets($f); //echo $address; $address = trim($line); //新建http连接事件到base $conn = new EventHttpConnection($base, $dns_base, $address, $port); $conn->setTimeout(1); //设置请求回调 $req = new EventHttpRequest("_request_handler", $conn); $req->addHeader("Host", $address, EventHttpRequest::OUTPUT_HEADER); $req->addHeader("Content-Length", "0", EventHttpRequest::OUTPUT_HEADER); $conn->makeRequest($req, EventHttpRequest::CMD_GET, "/"); $pend_req++; } fclose($f); //事件主循环 $base->loop();
c语言版,代码如下:
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <evhttp.h> #include <event2> #include <event2> #include <event2> typedef struct my_struct_s my_struct_t; struct my_struct_s { struct evhttp_connection *conn; struct evhttp_request *req; struct evhttp_uri *uri; struct event *cleanup; }; struct event_base *Base_Primary; char *trimwhitespace(char *str) { char *end; // Trim leading space while(isspace(*str)) str++; if(*str == 0) // All spaces? return str; // Trim trailing space end = str + strlen(str) - 1; while(end > str && isspace(*end)) end--; // Write new null terminator *(end+1) = 0; return str; } void connection_free(int sock, short which, void *arg) { //printf("freeing connection!!! The socket's FD would have been closed when the HTTP request ended and the ->req object would have been free'd\n"); // Get our structure object my_struct_t *myStruct = arg; // Cleanup our properties event_free(myStruct->cleanup); evhttp_connection_free(myStruct->conn); evhttp_request_free(myStruct->req); evhttp_uri_free(myStruct->uri); // Free our custom structure free(myStruct); } void http_request_done(struct evhttp_request *req, void *arg){ // Get our custom struct my_struct_t *myStruct = arg; // Setup our timeout information (we delay 5 seconds) struct timeval Timeout; Timeout.tv_sec = 0; Timeout.tv_usec = 0; // Add this structure to our cleanup base to be cleaned up synchronously // TODO: Probably not the best way to cleanup and event, but it'l work for the purposes of illustration. // This way would ensure no race conditions exist, but it's probably not the most efficient depending on how many requests, etc we're dealing with. myStruct->cleanup = evtimer_new(Base_Primary, connection_free, (void *)myStruct); evtimer_add(myStruct->cleanup, &Timeout); //printf("http_request_done, we put our custom strucutre into a cleanup event to be freed!\n"); } int http_req(char *uri) { // Allocate our custom struture my_struct_t *myStruct = malloc(sizeof(my_struct_t)); // Create our EVHTP connection and request myStruct->uri = evhttp_uri_parse(uri); myStruct->conn = evhttp_connection_base_new(Base_Primary, NULL, uri, 80); myStruct->req = evhttp_request_new(http_request_done, myStruct); evhttp_add_header(evhttp_request_get_output_headers(myStruct->req), "Host", "localhost"); evhttp_add_header(evhttp_request_get_output_headers(myStruct->req), "Connection", "close"); evhttp_make_request(myStruct->conn, myStruct->req, EVHTTP_REQ_GET, uri); evhttp_connection_set_timeout(myStruct->req->evcon, 2); return 1; } // Define our primary function int main(int argc, char *argv[]) { // Initialize our bases Base_Primary = event_base_new(); char filename[] = "/tmp/50000.txt"; //文件名 FILE *fp; char StrLine[1024]; //每行最大读取的字符数 char *host; if((fp = fopen(filename,"r")) == NULL) //判断文件是否存在及可读 { printf("error!"); return -1; } while (!feof(fp)) { fgets(StrLine,1024,fp); //读取一行 host = StrLine; host = trimwhitespace(host); //printf("%s", host); //输出 http_req(host); } fclose(fp); // //event_base_loop(Base_Primary); event_base_dispatch(Base_Primary); // Free our primary base event_base_free(Base_Primary); return 1; }</event2></event2></event2></evhttp.h></unistd.h></signal.h></stdlib.h></ctype.h></string.h></stdio.h>
文章地址:
转载随意^^请带上本文地址!

Amphp框架是一个高效的PHP异步编程框架,它支持多种协议和组件,其中HTTP客户端是其其中一个核心组件。使用Amphp框架中的HTTP客户端,我们可以轻松地发送异步HTTP请求并处理响应,从而提升我们所构建的Web应用程序的性能和可扩展性。本文将介绍如何在Amphp框架中使用HTTP客户端。一、安装Amphp框架在开始使用Amphp框架的HTTP客户端前

PHP8.1新增的异步HTTP客户端随着互联网的快速发展,各种Web应用程序的性能也变得越来越重要。为了提供更好的用户体验,开发人员需要使用高效的工具和技术来处理各种网络请求。幸运的是,PHP8.1引入了一个全新的功能,即异步HTTP客户端,它允许我们以非阻塞的方式执行HTTP请求,从而提高应用程序的性能。通过异步HTTP客户端,我们可以在发送请求后继续执行

在现代网络应用中,HTTP客户端是至关重要的组成部分。它们可以用于访问RESTAPI,进行数据交换并执行远程过程调用。然而,一些常规的HTTP客户端实现可能会面临性能问题,例如网络延迟、处理大量请求等。Swoole,一种基于PHP的高性能网络库,可以有效地解决这些问题。在本文中,我们将探讨如何使用Swoole实现高性能的HTTP客户端。一、基础知识在我们深

Gin框架是一个轻量级的Web框架,其设计目的是提供高性能和高可用性的Web处理模式。在Gin框架中,HTTP客户端和连接池是非常重要的组成部分。本文将深入探讨Gin框架中HTTP客户端和连接池的底层实现细节。一、HTTP客户端HTTP客户端是Gin框架中发送HTTP请求的核心组件。在Gin框架中,HTTP客户端有很多种不同的实现方式,但是最常用的两种方式是

Go语言是近年来非常流行的一门编程语言,它被广泛应用于Web开发、系统编程、云计算等领域。在Go语言中使用HTTP协议进行网络通信是非常常见的场景,而为了方便地编写HTTP客户端程序,Go语言提供了标准库中自带的net/http包及其相关子包。不过,有时候我们在使用HTTP客户端库时却会遇到一些问题,例如程序无法正确获取到网络服务端返回的数据,或者客户端程序

如何使用Go语言中的HTTP客户端函数发送GET请求并解析响应?一、介绍HTTP客户端函数在Go语言中,标准库提供了用于实现HTTP客户端的函数。这些函数可以用于发送各种类型的HTTP请求,并解析响应。在本文中,我们将重点介绍如何使用HTTP客户端函数发送GET请求并解析响应。二、发送GET请求在Go语言中,发送GET请求可以通过http.Get()函数实现

随着Web应用程序的不断发展,HTTP客户端的需求也越来越重要,特别是在跨网络请求数据的情况下。在传统的PHP实现中,使用cURL等库进行网络请求是一种常见的方式,但其不支持异步请求,导致在大量并发请求时性能受到限制。Swoole是一款基于PHP的协程网络通信引擎,其提供了一种异步请求的方式,可以大大提高应用程序的性能。本文将介绍如何使用Swoole封装一个

Java已经成为今天最受欢迎的编程语言之一。然而,即使是最好的编程语言也会出现错误。其中一个常见的问题是Java11标准HTTP客户端错误。在本文中,我们将深入探讨Java11标准HTTP客户端错误的原因,处理和避免它们的方法。什么是Java11标准HTTP客户端错误?Java11标准HTTP客户端错误是指使用Java11标准HTTP客户端实现时,可能会遇到


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
