Python中用pycurl监控http响应时间脚本分享
最近需要对节点到源站自己做个监控,简单的ping可以检测到一些东西,但是http请求的检查也要进行,于是就研究了下pycurl。
pycurl是个用c语言实现的python 库,虽然据说不是那么pythonic,但是却很高效,它支持的协议居多:
supporting FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP. libcurl supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, kerberos, HTTP form based upload, proxies, cookies, user+password authentication, file transfer resume, http proxy tunneling and more!
这一堆协议已经很多了,我需要就是http一个,相对urlib来说,这个库可能更快些。
以下这个脚本是对某一个给定的url进行检查,并打印出http相应码,响应大小,建立连接时间,准备传输时间,传输第一个字节时间,完成时间。
#!/usr/bin/python # coding: UTF-8 import StringIO import pycurl import sys import os class Test: def __init__(self): self.contents = '' def body_callback(self,buf): self.contents = self.contents + buf def test_gzip(input_url): t = Test() #gzip_test = file("gzip_test.txt", 'w') c = pycurl.Curl() c.setopt(pycurl.WRITEFUNCTION,t.body_callback) c.setopt(pycurl.ENCODING, 'gzip') c.setopt(pycurl.URL,input_url) c.perform() http_code = c.getinfo(pycurl.HTTP_CODE) http_conn_time = c.getinfo(pycurl.CONNECT_TIME) http_pre_tran = c.getinfo(pycurl.PRETRANSFER_TIME) http_start_tran = c.getinfo(pycurl.STARTTRANSFER_TIME) http_total_time = c.getinfo(pycurl.TOTAL_TIME) http_size = c.getinfo(pycurl.SIZE_DOWNLOAD) print 'http_code http_size conn_time pre_tran start_tran total_time' print "%d %d %f %f %f %f"%(http_code,http_size,http_conn_time,http_pre_tran,http_start_tran,http_total_time) if __name__ == '__main__': input_url = sys.argv[1] test_gzip(input_url)
脚本运行效果
xu:~/curl$ python pycurl_test.py http://daxuxu.info/ http_code http_size conn_time pre_tran start_tran total_time 200 8703 0.748147 0.748170 1.632642 1.636552
pycurl 的一些响应信息:
(参考: http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html )
pycurl.NAMELOOKUP_TIME 域名解析时间 pycurl.CONNECT_TIME 远程服务器连接时间 pycurl.PRETRANSFER_TIME 连接上后到开始传输时的时间 pycurl.STARTTRANSFER_TIME 接收到第一个字节的时间 pycurl.TOTAL_TIME 上一请求总的时间 pycurl.REDIRECT_TIME 如果存在转向的话,花费的时间 pycurl.EFFECTIVE_URL pycurl.HTTP_CODE HTTP 响应代码 pycurl.REDIRECT_COUNT 重定向的次数 pycurl.SIZE_UPLOAD 上传的数据大小 pycurl.SIZE_DOWNLOAD 下载的数据大小 pycurl.SPEED_UPLOAD 上传速度 pycurl.HEADER_SIZE 头部大小 pycurl.REQUEST_SIZE 请求大小 pycurl.CONTENT_LENGTH_DOWNLOAD 下载内容长度 pycurl.CONTENT_LENGTH_UPLOAD 上传内容长度 pycurl.CONTENT_TYPE 内容的类型 pycurl.RESPONSE_CODE 响应代码 pycurl.SPEED_DOWNLOAD 下载速度 pycurl.SSL_VERIFYRESULT pycurl.INFO_FILETIME 文件的时间信息 pycurl.HTTP_CONNECTCODE HTTP 连接代码 pycurl.HTTPAUTH_AVAIL pycurl.PROXYAUTH_AVAIL pycurl.OS_ERRNO pycurl.NUM_CONNECTS pycurl.SSL_ENGINES pycurl.INFO_COOKIELIST pycurl.LASTSOCKET pycurl.FTP_ENTRY_PATH

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.
