4
votes

In a Python CGI script, I have lists of strings which are keys to a hash:

APPENDIX_WEBSITES = ['CJSHayward']
MAIN_WEBSITES = ['Alfresco',
  'Bible',
  'Fathers',
  'MyCollab',
  'Koha',
  'MediaWiki',
  'Moodle',
  'RequestTracker',
  'SuiteCRM',
  'TikiWiki',
  'Wordpress']
# The variable "data" is populated with a hash containing all above entries as keys.
sys.stderr.write(repr(MAIN_WEBSITES) + '\n')
sys.stderr.write(repr(APPENDIX_WEBSITES) + '\n')
sys.stderr.write(repr(MAIN_WEBSITES + APPENDIX_WEBSITES) + '\n')
for website in MAIN_WEBSITES + APPENDIX_WEBSITES:
    sys.stderr.write(website)

The Apache log faithfully records:

[Tue Aug 08 16:25:34.266769 2017] [cgi:error] [pid 16429] [client 127.0.0.1:40600] AH01215: ['Alfresco', 'Bible', 'Fathers', 'MyCollab', 'Koha', 'MediaWiki', 'Moodle', 'RequestTracker', 'SuiteCRM', 'TikiWiki', 'Wordpress']: /usr/local/websites/home/www/configure/index.cgi, referer: http://localhost/
[Tue Aug 08 16:25:34.267050 2017] [cgi:error] [pid 16429] [client 127.0.0.1:40600] AH01215: ['CJSHayward']: /usr/local/websites/home/www/configure/index.cgi, referer: http://localhost/
[Tue Aug 08 16:25:34.267268 2017] [cgi:error] [pid 16429] [client 127.0.0.1:40600] AH01215: ['Alfresco', 'Bible', 'Fathers', 'MyCollab', 'Koha', 'MediaWiki', 'Moodle', 'RequestTracker', 'SuiteCRM', 'TikiWiki', 'Wordpress', 'CJSHayward']: /usr/local/websites/home/www/configure/index.cgi, referer: http://localhost/
[Tue Aug 08 16:25:34.267490 2017] [cgi:error] [pid 16429] [client 127.0.0.1:40600] AH01215: AlfrescoAlfrescoBibleBibleFathersFathersMyCollabMyCollabKohaKohaMediaWikiMediaWikiMoodleMoodleRequestTrackerRequestTrackerSuiteCRMSuiteCRMTikiWikiTikiWiki: /usr/local/websites/home/www/configure/index.cgi, referer: http://localhost/

I am surprised that it seems to be iterating once over a single string (a redundant concatenation of the list), instead of iterating over what repr seems to recognize as a list of strings, which is what I intended.

How can I get the loop to iterate over 'Alfresco', 'Bible', etc. up through 'CJSHayward'?

1
It DID iterate over the individual website names. However, you wrote them out without any newlines, so they all got run together on output. If you want them as separate lines in the log, add a newline to them, just as you did for the first three lines.jasonharper
sys.stderr.write(repr(MAIN_WEBSITES) + '\n') is just writing the list as a string with a newline appended.GH05T
for website in MAIN_WEBSITES + APPENDIX_WEBSITES: sys.stderr.write(website) iterates the concatenated lists, but sys.stderr.write does not append a newline by default. So, it appears that you are getting concatenation of strings, but you're not. If you want a newline after each call to write, you must be explicit: sys.stderr.write('{}\n'.format(website))GH05T
You forgot the + '\n' inside your loop. Voting to close as typo.Mad Physicist
Use print(<whatever>, file=sys.stderr) for greater easejuanpa.arrivillaga

1 Answers

1
votes

As commenters have indicated, you must append a newline character (\n) to website in order for each website to appear on it's own line in the Apache log.

for website in MAIN_WEBSITES + APPENDIX_WEBSITES:
    sys.stderr.write(website + '\n')

That is will resolve your issue. However, it may interest you to know why this resolves the issue.

It's not crazy to expect that each call to sys.stderr.write would result its own line in the log. The reason this is not the case is because sys.stderr is a stream. A stream is an abstraction for data being sent somewhere. In this case, sys.stderr.write writes to the standard error stream, and your Apache log reads from the standard error stream. The Apache log does not know how many times you have called sys.stderr.write. It only knows what it can see in the stream. For example, it is impossible for the Apache log to know the difference between

sys.stderr('HelloWorld')

and

sys.stderr('Hello')
sys.stderr('World')

In both cases, Apache log (and other applications that read from standard error) just see the stream of characters 'HelloWorld'. For this reason, Apache log must use the newline character (\n) to divide the standard error stream into separate lines.