开发者社区> 问答> 正文

Python相当于`curl -v http:// example.com /`

我一直在尝试使用不同的Python模块pycurl,requests但仍然无法获得curl -v 输出,如下所示。

期望的输出(在Python代码中)

C:>curl -v http://example.com/

  • Trying 93.184.216.34...
  • TCP_NODELAY set
  • Connected to example.com (93.184.216.34) port 80 (#0)

GET / HTTP/1.1
Host: example.com
User-Agent: curl/7.52.1
Accept: /

< HTTP/1.1 200 OK

< Cache-Control: max-age=604800
< Content-Type: text/html; charset=UTF-8
< Date: Mon, 21 Jan 2019 00:34:32 GMT
< Etag: "1337+ident"
< Expires: Mon, 28 Jan 2019 00:34:32 GMT
< Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
< Server: ECS (sjc/4E29)
< Vary: Accept-Encoding
< X-Cache: HIT
< Content-Length: 1270
<


... input truncated ...

  • Curl_http_done: called premature == 0
  • Connection #0 to host example.com left intact

C:>
由于这是在Windows中,我不想使用os.system和默认情况下不存在的subprocess模块curl.exe。

这是我的尝试...但我仍然没有获得类似的输出 curl -v

import requests
requests.get("http://example.com").content
requests.get("http://example.com").text

import pycurl
c = pycurl.Curl()
c.setopt(c.URL, 'http://example.com')
c.perform()

展开
收起
一码平川MACHEL 2019-01-22 10:49:43 2532 0
2 条回答
写回答
取消 提交回答
  • image.png

    2019-11-22 14:00:38
    赞同 展开评论 打赏
  • 如果您正在寻找有关等的信息Last-Modified,可以查看。Cache-Controlrequestsheaders

    import requests
    req = requests.get("http://example.com")
    req.headers
    {'Content-Encoding': 'gzip',

    'Accept-Ranges': 'bytes',
    'Cache-Control': 'max-age=604800',
    'Content-Type': 'text/html; charset=UTF-8',
    'Date': 'Mon, 21 Jan 2019 01:13:53 GMT',
    'Etag': '"1541025663"',
    'Expires': 'Mon, 28 Jan 2019 01:13:53 GMT',
    'Last-Modified': 'Fri, 09 Aug 2013 23:54:35 GMT',
    'Server': 'ECS (dca/24A0)',
    'Vary': 'Accept-Encoding',
    'X-Cache': 'HIT',
    'Content-Length': '606'}


    没有任何第三方软件包(例如requests),在Python 3.7.2中:

    import urllib.request
    dict(urllib.request.urlopen("http://example.com").headers)

    {'Accept-Ranges': 'bytes', 'Cache-Control': 'max-age=604800', 'Content-Type': 'text/html; charset=UTF-8', 'Date': 'Mon, 21 Jan 2019 02:52:13 GMT', 'Etag': '"1541025663+gzip"', 'Expires': 'Mon, 28 Jan 2019 02:52:13 GMT', 'Last-Modified': 'Fri, 09 Aug 2013 23:54:35 GMT', 'Server': 'ECS (sjc/4E8B)', 'Vary': 'Accept-Encoding', 'X-Cache': 'HIT', 'Content-Length': '1270', 'Connection': 'close'}

    2019-07-17 23:26:10
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载