I've got a web server that will take scripts in Python, PHP or Perl. I don't know much about any of those languages, but of the three, Python seems the least scary. It has a MySql database set up, and I know enough SQL to manage it and write queries for it.
I also have a program that I want to add automated error reporting to. Something goes wrong, it sends a bug report to my server.
What I don't know how to do is write a Python script that will sit on the web server and, when my program sends in a bug report, do the following:
- Receive the bug report.
- Parse it out into sections.
- Insert it into the database.
- Have the server send me an email.
From what little I understand, this seems like it shouldn't be too difficult if I only knew what I was doing. Could someone point me to a开发者_JAVA百科 site that explains the basic principles I'd need to create a script like this?
I don't know python so I am showing you php.
So assuming your bug sending code posts to file.php?report=report+is+here
The following will work.
<?
# code to initialize the database here
$rc = mysql_connect(...);
if (!$rc)
{
die ("Could not connect to the database.");
}
// probably should do some error checking in a try .. catch
mysql_execute("
INSERT INTO BugTable(report)
VALUES('".addslashes($_REQUEST['report'])."')");
mail("youremail@set.com","Bug Report Recieved", "Recieved the following bug report: {$_REQUEST['report']}");
?>
Everything about php is at php.net.
With Python, it depends on your web hosting. Some shared web hosts do PHP and then only do Perl/Python (and Ruby etc.) through CGI. With Python, you can build web stuff either using the CGI model or - if you have hosting that supports it - WSGI. If you are going to do Python and CGI, just look in the documentation for the cgi module. Alternatively, use web.py. You basically need to read in the input as an HTTP POST message on a particular URI. The web.py documentation should describe how to write this.
For the parsing - depends on what format it is being sent as. You can use x-www-form-encoded to transmit simple key-value pairs. I don't know the intricacies of web.py (I use Ruby mostly), but it should basically provide you with a way of getting a 'request' object, doing something with it and then you modify a 'return' object which contains what goes back to the browser. With web.py, this is web.input() - see here.
For anything more complicated, you need to basically POST the data in your chosen format - XML, JSON, magical binary blob format. How you parse that depends on what it is. Simply Google for "python xml" or "python json" or whatever and you'll find the latest library for it.
Inserting into the database - use the mysqldb library for Python (I use Postgres, mostly from Ruby and Java, so I'm not fully hip to the latest Python MySQL libraries).
For sending e-mail, you can either just use sendmail (on Unix systems with a sendmail installation) or you can use Python's smtplib to send it to an SMTP server - if you are just doing admin e-mails, use something like a Gmail account as your SMTP server as you know that it is going to work. Oh, web.py makes it easy - it has a built-in mail module. Use that, I guess.
You probably need to think about security - that means authentication and it means form validation. RTFM for whatever language and database library you use so you don't open yourself up to SQL injection - and do the same for e-mail. You don't want to be a proxy for spam.
精彩评论