python 模拟浏览器下载文件-Python 模拟浏览器 获取SVN 文件

1.[代码]GetFilesFromSVN.py

#----------------------------------------------

# Author : Jeff Yu

# Date : 2012-8-13

# Function : get files from SVN

#----------------------------------------------

#----------------------------------

# Step1: Get INFO

#----------------------------------

import ConfigParser,sys

try:

configFile = open("config.ini","r")

except IOError:

print "config.ini is not found"

raw_input("")

sys.exit()

config = ConfigParser.ConfigParser()

config.readfp(configFile)

configFile.close()

# get baseurl

try:

baseurl = config.get("INFO","baseurl")

# incase last "/" is missing in baseurl

baseurl = baseurl.rstrip("/")

baseurl = "%s/"%baseurl

except ConfigParser.NoOptionError:

print "baseurl is not found under section INFO in config.ini."

raw_input("")

sys.exit()

# get user

try:

user = config.get("INFO","user")

except ConfigParser.NoOptionError:

meg = "user is not found under section INFO in config.ini."

raw_input("")

sys.exit()

# get passwd

try:

passwd = config.get("INFO","passwd")

except ConfigParser.NoOptionError:

meg = "passwd is not found under section INFO in config.ini."

raw_input("")

sys.exit()

# get fileList

try:

fileList = config.get("INFO","fileList")

except ConfigParser.NoOptionError:

meg = "fileList is not found under section INFO in config.ini."

raw_input("")

sys.exit()

#----------------------------------

# Step2: Auth

#----------------------------------

import urllib2

realm = "Subversion Repositories"

auth = urllib2.HTTPBasicAuthHandler()

auth.add_password(realm, baseurl, user, passwd)

opener = urllib2.build_opener(auth, urllib2.CacheFTPHandler)

urllib2.install_opener(opener)

#----------------------------------

# Step3: Create Folder

#----------------------------------

import os

folderName = "svnFile"

if not os.path.exists(folderName):

os.mkdir(folderName)

#----------------------------------

# Step4: Get Files

#----------------------------------

fr = open(fileList,'r')

for i in fr:

i = i.strip(" ")

i = i.strip(" ")

# ignore the blank line

if i != "":

url = "%s%s"%(baseurl,i)

try:

data = urllib2.urlopen(url)

fw = open("%s/%s"%(folderName,i),'w')

fw.write(data.read())

fw.close()

print "Download: %s."%i

except urllib2.HTTPError, e:

# HTTPError is a subclass of URLError

# need to catch this exception first

mesg = str(e).split(" ")

errCode = mesg[2].rstrip(":")

if errCode == "401":

# HTTP Error 401: basic auth failed

print "Can not login in, please check the user and passwd in config.ini."

break

elif errCode == "404":

# HTTP Error 404: Not Found

print "Not Found: %s"%i

else:

print e

print "Failed to download %s"%i

except urllib2.URLError:

# 1.SVN server is down

# 2.URL is not correct

print "Please check SVN Server status and baseurl in config.ini."

break

fr.close()

raw_input("")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值