Created
November 5, 2019 11:58
-
-
Save anerdev/3bdff08f00c67bf5660b3a443bc500fe to your computer and use it in GitHub Desktop.
multiple website, multiple files to generate and upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ftplib | |
import os | |
import fileinput | |
import csv | |
# col1 = url (google.com), col2 = ftp host, col3 = ftp user, col4 = ftp password | |
with open('ftp.csv', 'rb') as csvfile: | |
ftp_records = csv.reader(csvfile, delimiter=',') | |
for row in ftp_records: | |
web_url = row[0] | |
print(web_url), | |
ftp_host = row[1] | |
print('\t' + ftp_host), | |
ftp_user = row[2] | |
print('\t' + ftp_user), | |
ftp_pass = row[3] | |
print('\t' + ftp_pass) | |
ftp = ftplib.FTP(ftp_host, ftp_user, ftp_pass) | |
file_list = ftp.nlst() | |
print('\tcerco www:'), | |
for n in file_list: | |
if n == 'www': | |
ftp.cwd('www') | |
print(' ok') | |
file_list = ftp.nlst() | |
file_list_lenght = len(file_list) | |
print('\tcerco index.html:'), | |
for n in file_list: | |
if n == 'index.html': | |
print(' ok'), | |
ftp.delete('index.html') | |
print(' --> cancellato,'), | |
print('\tcarico index.html:'), | |
file_upload = 'index.html' | |
file = open(file_upload, "rb") | |
ftp.storbinary('STOR ' + file_upload, file) | |
print(' ok') | |
print('\tcarico robots.txt:'), | |
file_upload = 'robots.txt' | |
file = open(file_upload, 'rb') | |
ftp.storbinary('STOR ' + file_upload, file) | |
print(' ok') | |
print('\tgenero sitemap.xml'), | |
sitemap_xml = open('sitemap.xml','w+') | |
sitemap_xml.write('<?xml version="1.0" encoding="UTF-8"?>') | |
sitemap_xml.write('\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">') | |
sitemap_xml.write('\n<url>') | |
sitemap_xml.write('\n<loc>https://' + row[0] + '/</loc>') | |
sitemap_xml.write('\n<lastmod>2019-11-04T09:00:00+00:00</lastmod>') | |
sitemap_xml.write('\n</url>') | |
sitemap_xml.write('</urlset>') | |
sitemap_xml.close() | |
print(' ok,'), | |
print('carico sitemap.xml:'), | |
file_upload = 'sitemap.xml' | |
file = open(file_upload, "rb") | |
ftp.storbinary('STOR ' + file_upload, file) | |
print(' ok') | |
print('\tgenero .htaccess:'), | |
htaccess = open('.htaccess','w+') | |
htaccess.write('RewriteEngine On') | |
htaccess.write('\nRewriteCond %{SERVER_PORT} 80') | |
htaccess.write('\nRewriteRule ^(.*)$ https://' + row[0] + '/$1 [R,L]') | |
htaccess.write('\nRewriteCond %{HTTP_HOST} ^www.' + row[0] +' [NC]') | |
htaccess.write('\nRewriteRule ^(.*)$ https://' + row[0] + '/$1 [L,R=301]') | |
htaccess.close() | |
print(' ok,'), | |
print('\tcarico .htaccess:'), | |
file_upload = '.htaccess' | |
file = open(file_upload, 'rb') | |
ftp.storbinary('STOR ' + file_upload, file) | |
print(' ok') | |
ftp.close() | |
print('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment