You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在pycurl中使用CURLOPT_REQUEST_TARGET选项?

在pycurl中使用CURLOPT_REQUEST_TARGET的方法

问题背景

想要在pycurl中实现curl命令行工具里--request-target选项的功能,尝试以下代码时出现属性不存在错误:

import pycurl
c = pycurl.Curl()
c.setopt(c.URL, "http://192.168.2.230")
c.setopt(c.REQUEST_TARGET, "*")
c.perform()
print(c.getinfo(c.HTTP_CODE))

报错内容:

AttributeError: trying to obtain a non-existing attribute: REQUEST_TARGET

改用CURLOPT_REQUEST_TARGET属性后,仍报类似错误:

AttributeError: trying to obtain a non-existing attribute: CURLOPT_REQUEST_TARGET

尝试通过选项编号(266)调用setopt_string的方式:

import pycurl
c = pycurl.Curl()
c.setopt(c.URL, "http://192.168.2.230")
c.setopt_string(266, "*")
c.perform()
print(c.getinfo(c.HTTP_CODE))

依然报错:

TypeError: strings are not supported for this option

最终解决方案

由于当时pycurl官方并未封装CURLOPT_REQUEST_TARGET这个libcurl选项,最终通过向pycurl项目提交pull request的方式,添加了对该功能的原生支持。


内容的提问来源于stack exchange,提问作者Marcel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 22:01:04