0

I couldn't find any similar SO threads, so i decided to create a new one.
I'd like to use loguru in my python scripts, but i encountered a problem with running them through url http://host:port/some/dirs/script.py
Running script like so gives me an internal server error
Server log says:

[Thu Aug 25 09:30:51.589337 2022] [cgi:error] [pid 18553] [client ...] AH01215: File "/some/dirs/script.py", line 3, in <module>
[Thu Aug 25 09:30:51.591079 2022] [cgi:error] [pid 18553] [client ...] End of script output before headers: script.py

But i can run script successfully with PyCharm or in terminal.
I suppose problem with server configuration, but i can't really change anything, since it's my work server (apache btw).
I would very much appreciate any info on this subject

  • Possibly your script crashes and there is no response created ? – Maurice Meyer Aug 25 '22 at 07:58
  • Apache expects web-server headers like `print("Content-type: text/html\r\n\r\n")` as the first output. Please see tutorials like: https://www.tutorialspoint.com/python/python_cgi_programming.htm. But I recommend using WSGI instead of CGI. – MortenB Aug 25 '22 at 08:06
  • @MortenB, yes, i have those, but since imports must be at the top of the file, and i have an issue with importing loguru, it results into End of script output before headers. I have other scripts, where i print headers after import statements, and those work fine – Edward Vlasenko Aug 25 '22 at 09:29
  • @MauriceMeyer, as i stated above, script runs successfully if i use terminal. It is a simple test script `from loguru import logger print(f'Content-Type: text/html') print() logger.add('logs/local_test.log', format='{time} {level} {message}', level='DEBUG') logger.debug('This is a debug message')` – Edward Vlasenko Aug 25 '22 at 09:30
  • @Edward Vlasenko: This seems not to be a valid CGI script. – Maurice Meyer Aug 25 '22 at 09:39
  • @MauriceMeyer, i fail to see the problem with the script. I'm not sure what you mean by CGI script. I just want to be able to run my python script using cron or let somebody run my script using a link. It works fine usually, but importing loguru crashes my script execution – Edward Vlasenko Aug 25 '22 at 09:52
  • You need to debug: Is the Python interpreter used within Apache able to import loguru ? Can the logger write into `logs/local_test.log`, what is `logs` relative too (I haven't used Apache since decades) ? ... – Maurice Meyer Aug 25 '22 at 10:26
  • @MauriceMeyer, I use the same python3.8 interpreter to run my flask app, where i also use loguru and it works fine. If i run `python3.8 script.py` it will successfully make a record to logs/local_test.log. logs/ and script.py are in the same directory – Edward Vlasenko Aug 25 '22 at 14:48

0 Answers0