1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #!/usr/bin/python
# -*- coding: utf-8 -*-
# IMPORTS
import urllib,os
from os import remove,rename
import os.path
import httplib
import socket
import re
# TIMEOUT
socket.setdefaulttimeout(10)
# LOCATION
the_location="/home/toto/xmlfiles/"
# XML
urls=["http://www.example.org/test.xml"
,"http://www.example.net/test.xml"
,"http://example.org/test.xml"
]
fichiers_url={} # Dictionary for url = fichier
# Download of test.xml files
for index,url in enumerate(urls):
urllib.urlcleanup()
# Nom du gull
nom = url
avirer = ["http://", "www.", ".org/", ".net/", "test.xml"]
for i in avirer :
nom = nom.replace(i,'')
# Fichier de destination
dest=os.path.join(the_location , nom + ".xml")
destfail=os.path.join(the_location , nom + ".FAIL.xml")
# Lecture préalable du header
for req in urls:
req = re.sub('^.*//','',req)
req = re.sub('/.*$','',req)
conn = httplib.HTTPConnection(req)
conn.request("GET", "/test.xml")
r2 = conn.getresponse()
# Prints
print req,r2.status,r2.reason
print dest
print ""
data2 = r2.read()
conn.close()
# Téléchargement et sauvegarde du xml sur le disque
try:
urllib.urlretrieve(url,dest)
# Ajout au dico de liaison url / fichier
fichiers_url[url]=dest
except:
print destfail + " haz a 3rr0r"
open(destfail,"w") |
Partager