Python网络编程基础笔记-URL获取页面


               
               
                # -*- coding: cp936 -*-
"""
演示了使用urlopen打开http/ftp/gopher地址
"""
import sys,urllib2
# http
req = urllib2.Request("http://www.baidu.com")
fd = urllib2.urlopen(req)
while 1:
    data = fd.read(1024)
    if not len(data):
        break
    sys.stdout.write(data)
# ftp
req = urllib2.Request("ftp://ftp.tw.debian.org/debian/")
fd = urllib2.urlopen(req)
while 1:
    data = fd.read(1024)
    if not len(data):
        break
    sys.stdout.write(data)
# gopher
req = urllib2.Request("gopher://gopher.quux.org/")
fd = urllib2.urlopen(req)
while 1:
    data = fd.read(1024)
    if not len(data):
        break
    sys.stdout.write(data)