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 "", line 1, in 
      File "/tmp/pip-install-b8co1_ap/pycurl/setup.py", line 970, in 
        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()

你可能感兴趣的:(python,python,开发语言)