python使用pycurl模块详解

来源:互联网 发布:液压系统仿真软件 编辑:程序博客网 时间:2024/05/22 15:48
  1. #!/usr/bin/env python2
  2. #encoding=utf8
  3. import pycurl
  4. import StringIO
  5. # 安装pycurl到http://pycurl.sourceforge.net/这里去找.
  6. # 在windows安装的话http://pycurl.sourceforge.net/download/ , 看你使用的版本决定下载那个,我在 windows使用的是python2.4, 所以下载 pycurl-ssl-7.15.5.1.win32-py2.4.exe 。
  7. def test(debug_type, debug_msg):
  8. print "debug(%d): %s" % (debug_type, debug_msg)
  9. def postFile(url,post_file):
  10. #print pycurl.version_info()
  11. #这个函数创建一个同 libcurl中的CURL处理器相对应的Curl对象.Curl对象自动的设置CURLOPT_VERBOSE为0, CURLOPT_NOPROGRESS为1,提供一个默认的CURLOPT_USERAGENT和设置CURLOPT_ERRORBUFFER指向一个私有的错误缓冲区.
  12. c = pycurl.Curl() #创建一个同libcurl中的CURL处理器相对应的Curl对象
  13. b = StringIO.StringIO()
  14. #c.setopt(c.POST, 1)
  15. c.setopt(pycurl.URL, url) #设置要访问的网址 url = "http://www.cnn.com"
  16. #写的回调
  17. c.setopt(pycurl.WRITEFUNCTION, b.write)
  18. c.setopt(pycurl.FOLLOWLOCATION, 1) #参数有1、2
  19. #最大重定向次数,可以预防重定向陷阱
  20. c.setopt(pycurl.MAXREDIRS, 5)
  21. #连接超时设置
  22. c.setopt(pycurl.CONNECTTIMEOUT, 60) #链接超时
  23. # c.setopt(pycurl.TIMEOUT, 300) #下载超时
  24. # c.setopt(pycurl.HEADER, True)
  25. # c.setopt(c.HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded","X-Requested-With:XMLHttpRequest","Cookie:"+set_cookie[0]])
  26. #模拟浏览器
  27. c.setopt(pycurl.USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)")
  28. # c.setopt(pycurl.AUTOREFERER,1)
  29. # c.setopt(c.REFERER, url)
  30. # cookie设置
  31. # Option -b/--cookie <name=string/file> Cookie string or file to read cookies from
  32. # Note: must be a string, not a file object.
  33. # c.setopt(pycurl.COOKIEFILE, "cookie_file_name")
  34. # Option -c/--cookie-jar Write cookies to this file after operation
  35. # Note: must be a string, not a file object.
  36. # c.setopt(pycurl.COOKIEJAR, "cookie_file_name")
  37. # Option -d/--data HTTP POST data
  38. post_data_dic = {"name":"value"}
  39. c.setopt(c.POSTFIELDS, urllib.urlencode(post_data_dic))
  40. #设置代理
  41. # c.setopt(pycurl.PROXY, ‘http://11.11.11.11:8080′)
  42. # c.setopt(pycurl.PROXYUSERPWD, ‘aaa:aaa’)
  43. #不明确作用
  44. # c.setopt(pycurl.HTTPPROXYTUNNEL,1) #隧道代理
  45. # c.setopt(pycurl.NOSIGNAL, 1)
  46. #设置post请求, 上传文件的字段名 上传的文件
  47. #post_file = "/home/ubuntu/avatar.jpg"
  48. c.setopt(c.HTTPPOST, [("textname", (c.FORM_FILE, post_file))])
  49. # 调试回调.调试信息类型是一个调试信 息的整数标示类型.在这个回调被调用时VERBOSE选项必须可用
  50. # c.setopt(c.VERBOSE, 1) #verbose 详细
  51. # c.setopt(c.DEBUGFUNCTION, test)
  52. # f = open("body", "wb")
  53. # c.setopt(c.WRITEDATA, f)
  54. # h = open("header", "wb")
  55. # c.setopt(c.WRITEHEADER, h)
  56. # print "Header is in file 'header', body is in file 'body'"
  57. # f.close()
  58. # h.close()
  59. # c.setopt(c.NOPROGRESS, 0)
  60. # c.setopt(c.PROGRESSFUNCTION, progress)
  61. # c.setopt(c.OPT_FILETIME, 1)
  62. #访问,阻塞到访问结束
  63. c.perform() #执行上述访问网址的操作
  64. print "HTTP-code:", c.getinfo(c.HTTP_CODE) #打印出 200(HTTP状态码)
  65. print "Total-time:", c.getinfo(c.TOTAL_TIME)
  66. print "Download speed: %.2f bytes/second" % c.getinfo(c.SPEED_DOWNLOAD)
  67. print "Document size: %d bytes" % c.getinfo(c.SIZE_DOWNLOAD)
  68. print "Effective URL:", c.getinfo(c.EFFECTIVE_URL)
  69. print "Content-type:", c.getinfo(c.CONTENT_TYPE)
  70. print "Namelookup-time:", c.getinfo(c.NAMELOOKUP_TIME)
  71. print "Redirect-time:", c.getinfo(c.REDIRECT_TIME)
  72. print "Redirect-count:", c.getinfo(c.REDIRECT_COUNT)
  73. # epoch = c.getinfo(c.INFO_FILETIME)
  74. #print "Filetime: %d (%s)" % (epoch, time.ctime(epoch))
  75. html = b.getvalue()
  76. print(html)
  77. b.close()
  78. c.close()