最近需要对节点到源站自己做个监控,简单的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

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software