Python http 模块

#url代码获取函数
def  geturl( url, refer="www.aiwaly.com" ):   
                send_headers = {      
                      'User-Agent':'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0',
                      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                      'Accept-Charset': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                      'Accept-Encoding': 'none',
                      'Accept-Language': 'text/html; charset=UTF-8',
                      'Connection':'keep-alive'
                     }

                req = urllib2.Request(url,headers=send_headers)
                #req.add_header( "Cookie" , "aaa=bbbb" ) # 这里通过add_header方法很容易添加的请求头       
                #req.add_header( "Host",'search.dangdang.com')

                req.add_header( "Referer", refer)
                try:
                      #res = urllib2.urlopen( req , 'timeout=' + str(timeout) )
                      res = urllib2.urlopen( req , timeout=5 )
                except urllib2.HTTPError,e:
                      print e.code
                      print e.read()
                      res = urllib2.urlopen( req , timeout=5 )

                html = res.read()
                res.close()
                #print html
                return html


import urllib ,urllib2           
def post(url, data):  
    req = urllib2.Request(url)  
    data = urllib.urlencode(data)  
    #enable cookie  
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())  
    response = opener.open(req, data)  
    return response.read()  
#文件下载模块  
import urllib2
def downloadfile(url,filename):
    #print "downloading with urllib2"
    #"url = 'http://www.pythontab.com/test/demo.zip' 
    try:
        f = urllib2.urlopen(url, timeout=10) 
        data = f.read() 
        with open(filename, "wb") as code:     
             code.write(data)
        f.close
        return  True 

    except:
        return  False
#文件下载hash目录存储
import sys ,os ,urllib2
def wget_file_hash(subjectname ,url ,imgfilename):   
        ##判断临时目录是否存在
        hashdir=get_md5_value(imgfilename)
        subjectpah=sys.path[0]+'/../../sources/' +subjectname + "/" +hashdir[0] + hashdir[1] + "/"+hashdir[1] + hashdir[2] + "/"
        print subjectpah        
        if not os.path.isdir(subjectpah):
                 os.makedirs(subjectpah)

        print downloadfile(url,subjectpah + imgfilename)
#使用样例
#wget_file_hash("subjectdir" ,"http://www.duba.com/static/v2/images/blank.gif" ,"blank.gif")

文档更新时间: 2019-06-14 13:36   作者:月影鹏鹏