[Python]-urllib and URLLIB2 modules

Source: Internet
Author: User
Tags urlencode

Transferred from: http://zeping.blog.51cto.com/6140112/1143722

Urllib and URLLIB2 are all related modules that accept URL requests, but provide different functionality.

Urllib provides the UrlEncode method for generating a Get query string, and Urllib2 does not.

Urllib:

Web Base:

  1. Import Urllib
  2. #打开51cto
  3. CTO = Urllib.urlopen (' http://www.51cto.com ')
  4. #打开本地文件: CTO = Urllib.urlopen (url= ' file:/root/python/ulib ')
  5. #打开ftp: CTO = Urllib.urlopen (url= ' ftp://username: password @ftp address/')
  6. #读取51cto首页的代码
  7. Print Cto.read ()
  8. #获取远程服务器返回的头信息, almost like Curl-i www,51cto.com.
  9. Print Cto.info ()
  10. #返回http状态码, 200 indicates success, 404 means URL not found
  11. Print Cto.getcode ()
  12. #返回请求的URL
  13. Print Cto.geturl ()
  14. #运行结果
  15. [[email protected] python] #python Ctourl
  16. 。。。。。。。 Omit Cto.read () function, output too much .....
  17. Server:tengine #cto. info () return information
  18. date:wed, Feb ::
  19. Content-type:text/html
  20. Connection:close
  21. Vary:accept-encoding
  22. Load-balancing:web48
  23. #cto. GetCode () return information
  24. http://www. 51cto.com #cto. Geturl () return information
  25. #urlopen返回的是一个类文件对象, and the way this object is used and the file object's
  26. #使用方法完全一样.

Encoding and decoding of characters:

  1. Import Urllib,os
  2. #对字符串进行编码
  3. Stra = Urllib.quote (' This is Python ')
  4. Print Stra
  5. #对字符串进行解码
  6. Print Urllib.unquote (stra)
  7. #这个方法用 ' + ' instead of%20 and urllib.quote similar,
  8. STRB = Urllib.quote_plus (' This is Python ')
  9. Print STRB
  10. #解码
  11. Print Urllib.unquote_plus (STRB)
  12. dicta = {' name ': 'zeping ',' passwd ':' 123456 '}
  13. #urlencode将字典转换成url参数
  14. Print Urllib.urlencode (dicta)
  15. #将本地路径转换成url路径
  16. filename = Urllib.pathname2url ('/python/test.py ')
  17. Print filename
  18. #将url路径转换成本地路径
  19. Print urllib.url2pathname (filename)
  20. ######### #运行结果 ##########
  21. [[email protected] python] # Python Quote
  22. this%20is%20python
  23. This is python
  24. this+Is+python
  25. This is python
  26. passwd=123456&name=zeping
  27. /python/test.py
  28. /python/test.py

Urllib.urlretrieve (): Download

  1. Import Urllib
  2. def Schedule (a,b,c):
  3. "' "
  4. A: Data blocks that have already been downloaded
  5. B: Size of the data block
  6. C: The size of the remote file
  7. ‘‘‘
  8. per = 100.0 * A * b/c
  9. if per > :
  10. per =
  11. print '%.2f%% '% per
  12. #这里以下载缓存插件为例
  13. url = ' http://fastlnmp.googlecode.com/files/eaccelerator-0.9.6.tar.bz2 '
  14. #获取文件名, here is the download to the current directory, if you want to download to another directory must
  15. #须输入绝对路径和文件名字:/root/tools/eaccelerator-0.9.6.tar.bz2
  16. Local = Url.split ('/') [-1]
  17. Urllib.urlretrieve (Url,local,schedule)
  18. ######## #运行结果 ##########
  19. [[email protected] urllib] # python down
  20. 0%
  21. 7.74%
  22. 15.48%
  23. 23.22%
  24. 30.96%
  25. 38.7%
  26. 46.44%
  27. 54.18%
  28. 61.92%
  29. 69.66%
  30. 77.4%
  31. 85.15%
  32. 92.89%
  33. 100%
  34. [[email protected] urllib] # ls
  35. Down eaccelerator-0.9. 6.tar.bz2 ulib2

URLLIB2:

Urllib2 can accept an instance of the request class to set the URL request headers,urllib only the URL can be accepted.

Above transfer from: http://zeping.blog.51cto.com/6140112/1143722

[Python]-urllib and URLLIB2 modules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.