python pycurl 安装使用

python pycurl 安装使用

本文主要讲下pycurl 安装使用.

1.安装

首先使用 pip 命令安装.

pip install pycurl

输出如下:

Collecting pycurl
  Using cached pycurl-7.45.2.tar.gz (234 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-b8co1_ap/pycurl/setup.py'"'"'; __file__='"'"'/tmp/pip-install-b8co1_ap/pycurl/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-b8co1_ap/pycurl/pip-egg-info
         cwd: /tmp/pip-install-b8co1_ap/pycurl/
    Complete output (22 lines):
    Traceback (most recent call last):
      File "/tmp/pip-install-b8co1_ap/pycurl/setup.py", line 229, in configure_unix
        p = subprocess.Popen((self.curl_config(), '--version'),
      File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
        self._execute_child(args, executable, preexec_fn, close_fds,
      File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
        raise child_exception_type(errno_num, err_msg, err_filename)
    FileNotFoundError: [Errno 2] No such file or directory: 'curl-config'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-b8co1_ap/pycurl/setup.py", line 970, in <module>
        ext = get_extension(sys.argv, split_extension_source=split_extension_source)
      File "/tmp/pip-install-b8co1_ap/pycurl/setup.py", line 634, in get_extension
        ext_config = ExtensionConfiguration(argv)
      File "/tmp/pip-install-b8co1_ap/pycurl/setup.py", line 93, in __init__
        self.configure()
      File "/tmp/pip-install-b8co1_ap/pycurl/setup.py", line 234, in configure_unix
        raise ConfigurationError(msg)
    __main__.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory: 'curl-config'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

解决安装失败的步骤如下:

  1. sudo apt-get install libcurl4-openssl-dev
  2. sudo apt-get install python-dev
  3. sudo apt-get install libssl-dev
  4. pip install pycurl

此时,可以看到安装成功.

Collecting pycurl
  Using cached pycurl-7.45.2.tar.gz (234 kB)
Building wheels for collected packages: pycurl
  Building wheel for pycurl (setup.py) ... done
  Created wheel for pycurl: filename=pycurl-7.45.2-cp38-cp38-linux_x86_64.whl size=341360 sha256=xxx
  Stored in directory: /home/zh/.cache/pip/wheels/07/65/79/xxx
Successfully built pycurl
Installing collected packages: pycurl
Successfully installed pycurl-7.45.2

2: 使用

pycurl是一个基于libcurl库的Python模块,它支持多种网络协议的,包括HTTP、HTTPS等。

使用pycurl可以进行网络请求、文件上传、下载等操作。

下面是使用get请求 , 访问百度. 并打印response.

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
 @Author: zh
 @Time 2023/12/22 下午12:56  .
 @Email:
 @Describe:
"""
import pycurl
# 创建一个Curl对象
curl = pycurl.Curl()
# 设置请求的URL
ccurlsetopt(curl.URL, 'http://www.baidu.com')
# 设置请求的方法为GET
curl.setopt(curl.HTTPGET, 1)
# 执行请求
curl.perform()

# 获取响应内容
response = curl.getinfo(curl.RESPONSE_CODE)
print(response)

# 关闭Curl对象
curl.close()
  • 12
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值