Новый для кодирования в python и ищет некоторую помощь в получении списка доменов и добавлении к ним http и https и проверке заголовка Location, где находится этот пункт назначения. Когда он читает в файле, он добавляет CR% 0a в конце запроса, не уверен, как удалить это:
import requests
filename = "urls.txt"
urls = open(filename, 'r')
def redirectTest(domTest):
try:
r = requests.head(domTest, allow_redirects=False)
if (r.status_code == 301):
print "Request:",domTest,"\t","Response:",r.status_code,"\t","Location:",r.headers['Location']
elif (r.status_code == 302):
print "Request:",domTest,"\n\t",r.status_code,"\t","Location:",r.headers['Location']
else:
print "Request:",domTest,"\t",r.url,"\t",r.status_code
except requests.exceptions.RequestException as e:
print "Request:",domTest,"\n\t",e
pass
def concatenateDom(dom):
mylist = ['http://', 'https://']
# mylist = ['http://', 'https://']
for a in mylist:
domTest = a + dom
## print domTest
redirectTest(domTest)
for a in urls:
concatenateDom(a)
urls.close()
Когда я передаю его в файл, это результаты, которые я получаю:
Запрос: http://google.com
HTTPConnectionPool(host='google.com%0a', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f540307f110>: Failed to establish a new connection: [Errno -2] Name or service not known',))
Запрос: https://google.com
HTTPSConnectionPool(host='google.com%0a', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f540307f1d0>: Failed to establish a new connection: [Errno -2] Name or service not known',))