首页 > 编程语言 > 详细

python网络学习(05)

时间:2016-03-17 12:20:19      阅读:207      评论:0      收藏:0      [点我收藏+]
# coding:utf-8
‘‘‘
Httplib微型浏览器
‘‘‘
import httplib

Remote_host = ‘www.python.org‘
Remote_Path = ‘/‘
USE_Aget =‘‘‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36‘‘‘


class HttpClent(object):
    def __init__(self, host):
        self.host = host

    def fetch(self, port):
        self.port = port
        http = httplib.HTTP(self.port)
        # preprader Header
        http.putrequest(‘GET‘, ‘/‘)
        http.putheader(‘User-Agent‘, USE_Aget)
        http.putheader(‘HOST‘, self.host)
        http.putheader(‘Accept‘, ‘*/*‘)
        http.endheaders()

        try:
             errcode, errmsg, header = http.getreply(True)
        except Exception, e:
           print ‘连接失败‘
        else:
           print ‘Got page from %s‘ % self.host
        file = http.getfile()
        return file.read()


if __name__ == "__main__":
    demo = HttpClent(Remote_host)
    print demo.fetch(Remote_Path)

  从HTPP站点下载文件

python网络学习(05)

原文:http://www.cnblogs.com/linbinqiang/p/5286624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!