最近需要对节点到源站自己做个监控,简单的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-8import StringIOimport pycurlimport sysimport osclass Test: def __init__(self): self.contents = '' def body_callback(self,buf): self.contents = self.contents + bufdef 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_time200 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_URLpycurl.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_VERIFYRESULTpycurl.INFO_FILETIME 文件的时间信息pycurl.HTTP_CONNECTCODE HTTP 连接代码pycurl.HTTPAUTH_AVAILpycurl.PROXYAUTH_AVAILpycurl.OS_ERRNOpycurl.NUM_CONNECTSpycurl.SSL_ENGINESpycurl.INFO_COOKIELISTpycurl.LASTSOCKETpycurl.FTP_ENTRY_PATH
新闻热点
疑难解答