python使用pycurl库

/

Mac安装pycurl

源码下载地址http://pycurl.sourceforge.net

  1. tar xvf pycurl-7.19.5.tar.gz
  2. cd pycurl-7.19.5
  3. sudo python setup.py install

测试pycurl是否安装成功 ,命令行执行python

  1. import pycurl
  2. print pycurl.version

如果你使用是pycharm,可以直接到preference里安装,如下图

pycurl_01

实例

这里的示例,实现一个从web server上下载一个文件到本地的功能,利用pycurl这个模块,直接上代码

  1. __author__ = 'djstava'
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. import pycurl
  4. import sys
  5. DJ_PYCURL_CONNECTTIMEOUT = 30
  6. DJ_PYCURL_TIMEOUT = 300
  7. DJ_PYCURL_DOWNLOADURL = "http://192.168.1.99/test.zip"
  8. DJ_PYCURL_DOWNLOAD_FILE = "download.file"
  9. fp = open(DJ_PYCURL_DOWNLOAD_FILE,'wb+')
  10. def dj_pycurl_writeFile(buffer):
  11. fp.write(buffer)
  12. def dj_pycurl_download(url):
  13. pycurl.global_init(pycurl.GLOBAL_ALL)
  14. c = pycurl.Curl()
  15. c.setopt(pycurl.URL, url)
  16. c.setopt(pycurl.WRITEDATA,fp)
  17. c.setopt(pycurl.WRITEFUNCTION,dj_pycurl_writeFile)
  18. c.setopt(pycurl.NOPROGRESS,0)
  19. c.setopt(pycurl.CONNECTTIMEOUT,DJ_PYCURL_CONNECTTIMEOUT)
  20. c.setopt(pycurl.TIMEOUT,DJ_PYCURL_TIMEOUT)
  21. c.setopt(pycurl.VERBOSE,1)
  22. c.perform()
  23. c.close()
  24. fp.close()
  25. if __name__ == '__main__':
  26. dj_pycurl_download(DJ_PYCURL_DOWNLOADURL)

代码测试

pycurl_02

Mac安装nginx

brew install nginx

修改nginx配置文件

/usr/local/etc/nginx/nginx.conf

nginx默认根目录

/usr/local/opt/nginx/html

启动nginx服务

sudo nginx

References

1、http://pycurl.sourceforge.net

转载请注明作者和出处,并添加本页链接。
原文链接: http://xugaoxiang.com/post/11

给我留言