我對python非常陌生,並且有一個腳本在電子郵件發送到服務器時運行。我的問題是試圖調試腳本。我如何將任何錯誤推送到日誌文件?python如何通過記錄問題來調試錯誤
下面是腳本:
#!/usr/bin/python
import sys, email
email_input = email.message_from_string(sys.stdin.read())
directory = "/home/someuser/files"
counter = 1
for part in email_input.walk():
# multipart/* are just containers
if part.get_content_maintype() == 'multipart':
continue
# Applications should really sanitize the given filename so that an
# email message can't be used to overwrite important files
filename = part.get_filename()
if not filename:
ext = mimetypes.guess_extension(part.get_content_type())
if not ext:
# Use a generic bag-of-bits extension
ext = '.bin'
filename = 'part-%03d%s' % (counter, ext)
counter += 1
fp = open(os.path.join(directory, filename), 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
HTTPS:/ /docs.python.org/2/howto/logging.html#logging-howto – wwii