cURL-03-cURL的几种语言支持

一、基于 C 的 HTTP 客户端

C API 在 libcurl 功能上提供了两个 API。easy 接口是一个简单的同步 API(意味着当您使用请求调用 libcurl 时,将能够满足您的请求,直到完成或发生错误)。多接口可以进一步控制 libcurl,您的应用程序可以执行多个同步传输,并控制 libcurl 何时何地移动数据。

该示例使用 easy 接口。该 API 还能控制数据移动过程(使用回调),但正如其名称所示,使用起来非常简单。下面请看HTTP 的 C 语言示例,使用 libcurl easy 接口的 C HTTP 客户端。

#include <stdio.h> 
#include <string.h> 
#include <curl/curl.h> 
 
#define MAX_BUF     65536 
 
char wr_buf[MAX_BUF+1]; 
int  wr_index; 
 
/* 
* Write data callback function (called within the context of 
* curl_easy_perform. 
*/ 
size_t write_data( void *buffer, size_t size, size_t nmemb, void *userp ) 
{ 
 int segsize = size * nmemb; 
 
 /* Check to see if this data exceeds the size of our buffer. If so, 
  * set the user-defined context value and return 0 to indicate a 
  * problem to curl. 
  */ 
 if ( wr_index + segsize > MAX_BUF ) { 
   *(int *)userp = 1; 
   return 0; 
 } 
 
 /* Copy the data from the curl buffer into our buffer */ 
 memcpy( (void *)&wr_buf[wr_index], buffer, (size_t)segsize ); 
 
 /* Update the write index */ 
 wr_index += segsize; 
 
 /* Null terminate the buffer */ 
 wr_buf[wr_index] = 0; 
 
 /* Return the number of bytes received, indicating to curl that all is okay */ 
 return segsize; 
} 
 
 
/* 
* Simple curl application to read the index.html file from a Web site. 
*/ 
int main( void ) 
{ 
 CURL *curl; 
 CURLcode ret; 
 int  wr_error; 
 
 wr_error = 0; 
 wr_index = 0; 
 
 /* First step, init curl */ 
 curl = curl_easy_init(); 
 if (!curl) { 
   printf("couldn't init curl\n"); 
   return 0; 
 } 
 
 /* Tell curl the URL of the file we're going to retrieve */ 
 curl_easy_setopt( curl, CURLOPT_URL, "www.exampledomain.com" ); 
 
 /* Tell curl that we'll receive data to the function write_data, and 
  * also provide it with a context pointer for our error return. 
  */ 
 curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)&wr_error ); 
 curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data ); 
 
 /* Allow curl to perform the action */ 
 ret = curl_easy_perform( curl ); 
 
 printf( "ret = %d (write_error = %d)\n", ret, wr_error ); 
 
 /* Emit the page if curl indicates that no errors occurred */ 
 if ( ret == 0 ) printf( "%s\n", wr_buf ); 
 
 curl_easy_cleanup( curl ); 
 
 return 0; 
}

最上方是必需的 include文件,包括 cURL 根文件。接下来,定义了两个用于传输的变量。第一个变量是 wr_buf,表示将在其中写入传入数据的缓冲区。wr_index表示缓冲区的当前写入索引。

转到 main函数,该函数使用 easy API 进行设置。所有 cURL 调用都通过维护特定请求状态的句柄进行操作。这称为 CURL指针引用。本例还创建一个特殊的返回码,称为 CURLcode。在使用任何 libcurl 函数之前,您需要调用 curl_easy_init获取 CURL句柄。接下来,注意 curl_easy_setopt调用的数量。它们为特定的操作配置句柄。对于这些调用,您提供句柄、命令和选项。首先,本例使用 CURLOPT_URL指定要获取的 URL。然后,它使用 CURL_WRITEDATA提供一个上下文变量(在本例中,它是内部的 write 错误变量)。最后,它使用 CURLOPT_WRITEFUNCTION指定数据可用时应该调用的函数。在启动 API 之后,API 将使用它读取的数据多次调用该函数。

要开始传输,调用 curl_easy_perform。它的工作是根据之前的配置执行传输。调用该函数时,在完成传输或发生错误之前该函数不会返回。main的最后一步是提交返回状态,提交页面读取,最后使用 curl_easy_cleanup清除(当使用句柄执行完操作后)。

现在看看 write_data函数。该函数是针对特定操作收到数据时调用的回调。注意,当您从网站读取数据时,将写入该数据(write_data)。将向回调提供一个缓冲区(包含可用数据)、成员数量和大小(缓冲中可用数据总量)、上下文指针。第一个任务是确保缓冲区(wr_buf)的空间足以写入数据。如果不够,它将设置上下文指针并返回 0,表示出现问题。否则,它将 cURL 缓冲区的数据复制到您的缓冲区,并增加索引,指向要写入的下一个位置。本例还终止字符串,稍后可以对其使用 printf。最后,它返回 libcurl 操作的字节数量。这将告诉 libcurl 数据被提取,它也可以丢弃该数据。这就是从网站将文件读取到内存的相对简单的方法。

二、基于 Python 的 HTTP 客户端

Python 是一种非常有用的面向对象的脚本语言,在原型化和构建生产软件方面非常突出。示例假设您较熟悉 Python,但使用不多,因此不要期望过高。

这个简单的 Python HTTP 客户端使用 pycurl,如下:

使用 libcurl 的 pycurl接口的 Python HTTP 客户端

import sys 
import pycurl 
 
wr_buf = ''
 
def write_data( buf ): 
    global wr_buf 
    wr_buf += buf 
 
def main(): 
    c = pycurl.Curl() 
    c.setopt( pycurl.URL, 'http://www.exampledomain.com' ) 
    c.setopt( pycurl.WRITEFUNCTION, write_data ) 
 
    c.perform() 
 
    c.close() 
 
main() 
sys.stdout.write(wr_buf)

这比 C 语言版本简单的多。它首先导入必需的模块(用于标准系统的 sys和 pycurl模块)。接下来,它定义 write 缓冲区(wr_buf)。像 C 程序中一样,声明一个 write_data函数。注意,该函数只有一个参数:从 HTTP 服务器中读取的数据缓冲区。我将该缓冲区连接到全局 write 缓冲区。main函数首先创建一个 Curl句柄,然后使用 setopt方法为传输定义 URL和 WRITEFUNCTION。它调用 perform方法启动传输并关闭句柄。最后,它调用 main函数,并将 write 缓冲区提交到 stdout。注意,在这种情况下,不需要错误上下文指针,因为使用了 Python 字符串连接,这就是说您不会使用大小固定的字符串。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值