Make CGI-script work (Apache, Python, MySQL) - python

Could you help me read some strings from MySQL and show them in my web browser.
This is just for my localhost.
I receive the following:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
As for my file hello.py, it is situated here:
root#ubuntu:/var/www/test1.my/www/cgi-bin# ls -l
total 8
It's properties are as follows:
-rwxr-xr-x 1 root root 465 Nov 2 23:30 hello.py
-rwxr-xr-x 1 root root 186 Nov 2 15:59 hello.py~
Could you give me some hint what to fix?
These are my configuration files

From what I see, you need to output an empty line after writing your Content-Type-header in hello.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
print "Content-Type: text/html\n"
# newline after headers! ^^
print "<html><head><title>Книги</title></head>"
...
Also, you should check the apache error logs (usually at /var/log/apache/error.log) to get more information about the error.

Related

Error while running python with xammp server

I am new in running python with xammp server. I have done all the required setup to run python in xammp server by following this link Running Python scripts with Xampp. While trying to running my code i always get an error.
**Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at postmaster#localhost to
inform them of the time this error occurred, and the actions you
performed just before this error.
More information about this error may be available in the server error
log.**
My error log says this:
[Tue Jan 19 10:29:34.529875 2021] [cgi:error] [pid 11652:tid 1860] [client ::1:50570] malformed header from script 'python.py': Bad header: Hello World
i am attaching the code for my file too.
#! C:\Users\"UserName"\AppData\Local\Programs\Python\Python39\python.exe
print("Content-Type: text/html\n\n")
import cgi
print("Hello Python")
i have tried everything i could to search for the solution but nothing is helping. Please help me finding a solution. Thank you
Update the \n\n to use a carriage return (\r\n\r\n), and add another in.
This should make your HTTP headers valid, as they may not be terminating correctly.
Edit: You can also try multi-line print instructions such as
print "Content-type: text/html"
print ""
This is interpreting the body as the header (never terminated) so terminating this correctly should fix it.

Python script won't play sound as cgi, but via command line

The CGI python script shown below, sound.py, plays a sound through a speaker connected to a Raspberry’s audio output. The sound plays fine when I execute the script from the command line via “python sound.py”. However, when I try to trigger it as a cgi script by invoking it via the Web (http://192.168.1.246/cgi-bin/sound.py), the sound won’t play, and the browser will only display “Hi Hello World!” (which, I suppose, indicates that the server at least recognizes it as a CGI script and executes it without producing errors).
I thought that the issue might have to do with the permissions/ownership of the sound file and tinkered around with those, but that didn’t help (I’m also pasting below information about the ownership and permission of the script itself, and the sound file).
Thanks a lot for helping!
Marc.
-rwxrwxrwx 1 root root 441 Jul 10 23:23 sound.py
-rwxr-xr-x 1 root root 29812 Jul 9 22:58 cardinal-short.mp3
Script (sound.py):
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import pygame
name = "cardinal-short"
pygame.mixer.init()
pygame.mixer.music.load("/var/www/html/cgi-bin/cardinal-short.mp3")
pygame.mixer.music.play()
pygame.mixer.music.set_volume(1.0)
while (pygame.mixer.music.get_busy() == True):
continue
pygame.mixer.quit()
print "Content-Type: text/plain;charset=utf-8"
print
print "Hi Hello World!"
I was able to fix this via:
sudo usermod -aG audio www-data
and then restarting apache.
The problem was that, and I quote from elsewhere on the web:
the sound devices (in /dev/snd/) are only accessible to members of group "audio". pi is a member of that group, and www-data is not.

Python CGI script running from web URL creates directory with www-data user

I am trying to run Python CGI script. The issue I am facing is, I need to create a directory and clone repo from git with my username.
#!/usr/bin/python2.7
import commands, os
print "Content-type: text/html\n"
print "\n\n"
print "<html><body>"
commands.getoutput("rm -rf fresh-cloned")
commands.getoutput("mkdir fresh-cloned")
os.chdir("fresh-cloned")
print commands.getoutput("pwd")
commands.getoutput("git clone <> -b <>")
But somehow, when I run the script from web URL : "http://ip_adr/webtest/webgui.py", I see the directory being created with www-data user rather than with my username. Due to that git clone won't work as ssh key is added in Stash for my username only.
drwxr-xr-x 2 www-data www-data 4.0K Nov 28 16:59 fresh-cloned
How can I resolve this issue? Is it possible to change the user to my username while running script from web URL?
If you want your script ran as your user from Apache HTTPD you need to load mod_suexec and set the directive SuexecUserGroup accordingly, such as:
SuexecUserGroup youruser yourgroup
I was able to resolve the issue by changing following line in "/etc/passwd"
www-data:x:33:33:www-data:/var/www:/bin/bash
I created/updated user, gave required permissions, created ssh key and added in stash, it is working now. Thanks!

Sendmail milter socket unsafe

I have set a Python milter for Sendmail
This is the entry in sendmail.mc according to sendmail specification
INPUT_MAIL_FILTER(`pyfilterplain', `S=local:/opt/PCIReaderMilter/Milters/plainMailSocket')dnl
I compiled .mc file with m4. Also python milter is running and created the socket
srwxr-xr-x. 1 root root 0 oct 11 03:24 plainMailSocket
in /opt/PCIReaderMilter/Milters
Just according to sendmail.mc entry
But when starting root#myvps# service sendmail start I get an error:
Starting sendmail: 451 4.0.0 /etc/mail/sendmail.cf: line 1707: Xpyfilterplain: local socket name /opt/PCIReaderMilter/Milters/plainMailSocket unsafe: Permission denied
I've googled and tried all solutions and recommendations, but all seems to be fine in my configuration. This same configuration works very well in Ubuntu 14.04, but not in CentOS 6.7
What am I doing wrong?
Well, I ended up disabling SELinux while waiting for a better solution
Sendmail checks permission of ALL ancestor directories on the socket path - too broad permissions are bad for security
(/opt/PCIReaderMilter/Milters/,/opt/PCIReaderMilter/,/opt/,/).
You may use one of DontBlameSendmail options as the last resort:
https://www.sendmail.com/sm/open_source/tips/DontBlameSendmail/

Why do I get a gnome permissions error when I run Selenium using PyVirtualDisplay from a Django website?

I’m trying to use Python’s Selenium bindings to programmatically interact with websites, as part of a Django website.
As my Selenium code runs as part of a Django website, by default (if I understand correctly) there is no display available for the browser to use. I’m thus trying to use PyVirtualDisplay to start Xvfb before my Selenium code runs.
Here‘s my code:
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=False, size=(800, 600))
display.start()
browser = webdriver.Firefox()
When I SSH into my server (running Debian Squeeze, Python 2.6.6, Selenium 2.25, PyVirtualDisplay 0.1.0), run the Python console as myself, and type in the code above, it works fine.
However, when I try to run that code from my Django site, or use su to run the Python console as www-data (which I believe is the user that Django runs as), I get the following error:
selenium.common.exceptions.WebDriverException: Message: 'The browser appears to have exited before we could connect. The output was:
(process:2963): Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.
Xlib: extension "RANDR" missing on display ":1082.0".
(firefox-bin:2963): libgnomevfs-WARNING **: Unable to create ~/.gnome2 directory: Permission denied
Could not create per-user gnome configuration directory `/var/www/.gnome2/\': Permission denied'
I’m a bit of a noob with Xvfb and Linux, so I’m not quite sure what I’m doing wrong.
I believe this is a simple permissions error.
On ubuntu apaches home directory, as you see, is /var/www
I think you just need to make sure that apache has write access to its home directory. My default on ubuntu 12.04 is
daniel#daniel:/var/www$ ls -la
total 12
drwxr-xr-x 2 root root 4096 Sep 15 11:43 .
drwxr-xr-x 14 root root 4096 Oct 2 08:54 ..
-rw-r--r-- 1 root root 177 Sep 15 11:43 index.html
www-data does not have write access to its own home directory!
Perhaps you could have www-data own the directory, or create an admin group that has write permission and add www-data to it?
Some other threads on giving write access to /var/www
https://superuser.com/questions/19318/how-can-i-give-write-access-of-a-folder-to-all-users-in-linux

Categories

Resources