IMO, сосредоточение внимания на коде/статусе выхода wget
может быть слишком наивным для некоторых случаев использования, поэтому здесь также рассматривается код состояния HTTP. для детального принятия решений.
wget
предоставляет флаг -S/--server-response
для вывода заголовков HTTP-ответа на STDERR
команды, которые мы можем извлечь и действовать в соответствии с ними. .
#!/bin/bash
set -eu
error_max=2
error_count=0
urls=( 'http://www.iqandreas.com/sample-images/100-100-color/'{90..110}'.jpg' )
for url in "${urls[@]}"; do
set +e
http_status=$( wget --server-response -c "$url" 2>&1 )
exit_status=$?
http_status=$( awk '/HTTP\//{ print $2 }' <<<"$http_status" | tail -n 1 )
if (( http_status >= 400 )); then
# Considering only HTTP Status errors
case "$http_status" in
# Define your actions for each 4XX Status Code below
410) : Gone
;;
416) : Requested Range Not Satisfiable
error_count=0 # Reset error_count in case of `wget -c`
;;
403) : Forbidden
;&
404) : Not Found
;&
*) (( error_count++ ))
;;
esac
elif (( http_status >= 300 )); then
# We're unlikely to reach here in case of 1XX, 3XX in $http_status
# but ..
exit_status=0
elif (( http_status >= 200 )); then
# 2XX in $http_status considered successful
exit_status=0
elif (( exit_status > 0 )); then
# Where wget's exit status is one of
# 1 Generic error code.
# 2 Parse error
# - when parsing command-line options, the .wgetrc or .netrc...
# 3 File I/O error.
# 4 Network failure.
# 5 SSL verification failure.
# 6 Username/password authentication failure.
# 7 Protocol errors.
(( error_count++ ))
fi
echo "$url -> http_status: $http_status, exit_status=$exit_status, error_count=$error_count" >&2
if (( error_count >= error_max )); then
echo "error_count $error_count >= $error_max, bailing out .." >&2
exit "$exit_status"
fi
done
Для этого вы можете использовать отладчик Python, pdb
. Вы можете импортировать его как модуль или использовать в интерактивном режиме. Документация для 2.7 находится здесь: https://docs.python.org/2/library/pdb.html
Вы можете использовать мой проект madbg . Это отладчик Python, который позволяет подключаться к работающей программе Python и отлаживать ее в текущем терминале. Он похож на pyrasite
и pyringe
, но поддерживает python3, не требует gdb и использует IPython
для отладчика (, что означает pdb с цветами и автозаполнением ).
Например, чтобы увидеть, где застрял ваш скрипт, вы можете запустить:
madbg attach <pid>
И в оболочке отладчика введите :bt