使用Python获取DNS解析时间和响应时间 [英] Getting DNS resolution time and response time with Python

查看:1056
本文介绍了使用Python获取DNS解析时间和响应时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PycURL或任何其他python pakcage是否提供有关:

Does PycURL or any other python pakcage provides information about :

  • 查找
  • 连接时间

我想获得与此cURL命令相同的信息(无需使用子进程调用该命令):

I would like to get the same information as this cURL command does (without calling the command using subprocess):

命令

curl -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null http://stackoverflow.com/

输出:

Lookup time:    0.029
Connect time:   0.144
PreXfer time:   0.144
StartXfer time: 0.263

Total time: 0.803

推荐答案

是的,PyCurl提供了该信息.您可以使用pycurl导出信息.它可以提供很多细节,而不仅仅是您提到的细节.

Yes, PyCurl provides the information. You can derive the information using pycurl. It can give a lot of details, not just the ones you mentioned.

以下是示例代码,可用于导出相同的信息:

Here's a sample code that you can use to derive the same information:

import pycurl
from io import BytesIO

buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://stackoverflow.com/')
c.setopt(c.WRITEDATA, buffer)
c.perform()
body = buffer.getvalue()

print('TOTAL_TIME: %f' % c.getinfo(c.TOTAL_TIME))
print('CONNECT_TIME: %f' % c.getinfo(c.CONNECT_TIME))
print('PRETRANSFER_TIME: %f' % c.getinfo(c.PRETRANSFER_TIME))
print('STARTTRANSFER_TIME: %f' % c.getinfo(c.STARTTRANSFER_TIME))

c.close()

它给出以下结果:

TOTAL_TIME: 2.252639
CONNECT_TIME: 0.331571
PRETRANSFER_TIME: 0.331634
STARTTRANSFER_TIME: 0.638206

我找到了 GitHub链接,其中提到了其他一些标志以及可以在您的代码中使用的代码. 以下是用于快速查看的标志:

I found a GitHub link that mentions some of the other flags as well which can be used in your code. Here are the flags for a quick view:

* EFFECTIVE_URL
* HTTP_CODE
* TOTAL_TIME
* NAMELOOKUP_TIME
* CONNECT_TIME
* PRETRANSFER_TIME
* REDIRECT_TIME
* REDIRECT_COUNT
* SIZE_UPLOAD
* SIZE_DOWNLOAD
* SPEED_UPLOAD
* HEADER_SIZE
* REQUEST_SIZE
* CONTENT_LENGTH_DOWNLOAD
* CONTENT_LENGTH_UPLOAD
* CONTENT_TYPE
* RESPONSE_CODE
* SPEED_DOWNLOAD
* SSL_VERIFYRESULT
* INFO_FILETIME
* STARTTRANSFER_TIME
* REDIRECT_TIME
* REDIRECT_COUNT
* HTTP_CONNECTCODE
* HTTPAUTH_AVAIL
* PROXYAUTH_AVAIL
* OS_ERRNO
* NUM_CONNECTS
* SSL_ENGINES
* INFO_COOKIELIST
* LASTSOCKET
* FTP_ENTRY_PATH

这篇关于使用Python获取DNS解析时间和响应时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆