如何检索自动报告并将其保存到数据库?

发布于 2024-08-25 16:25:48 字数 406 浏览 5 评论 0原文

我有一个 Web 服务器,可以使用 Python、PHP 或 Perl 编写脚本。我对这些语言中的任何一种都不太了解,但在这三种语言中,Python 似乎是最不可怕的。它设置了一个 MySql 数据库,并且我了解足够的 SQL 来管理它并为其编写查询。

我还有一个程序想要添加自动错误报告。出了问题,它向我的服务器发送了错误报告。

我不知道如何编写一个位于 Web 服务器上的 Python 脚本,当我的程序发送错误报告时,执行以下操作:

  • 接收错误报告。
  • 将其解析为几个部分。
  • 将其插入数据库。
  • 让服务器给我发一封电子邮件。

据我所知,如果我只知道自己在做什么,这似乎应该不会太困难。有人可以向我指出一个网站,该网站解释了创建这样的脚本所需的基本原则吗?

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 site that explains the basic principles I'd need to create a script like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

银河中√捞星星 2024-09-01 16:25:48

我不懂 python,所以我向你展示 php。

因此,假设您的错误将代码帖子发送到 file.php?report=report+is+here

以下内容将起作用。

<?

 # 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("[email protected]","Bug Report Recieved", "Recieved the following bug report: {$_REQUEST['report']}");

 ?>

关于 php 的一切都在 php.net 上。

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("[email protected]","Bug Report Recieved", "Recieved the following bug report: {$_REQUEST['report']}");

 ?>

Everything about php is at php.net.

莫多说 2024-09-01 16:25:48

对于 Python,这取决于您的网络托管。一些共享 Web 主机执行 PHP,然后仅通过 CGI 执行 Perl/Python(和 Ruby 等)。使用 Python,您可以使用 CGI 模型或 - 如果您有支持它的托管 - WSGI 来构建 Web 内容。如果您打算使用 Python 和 CGI​​,只需查看 cgi 模块的文档即可。或者,使用 web.py。您基本上需要将输入读入特定 URI 上的 HTTP POST 消息。 web.py 文档应该描述如何编写它。

对于解析 - 取决于它发送的格式。您可以使用 x-www-form-encoded 来传输简单的键值对。我不知道 web.py 的复杂性(我主要使用 Ruby),但它基本上应该为您提供一种获取“请求”对象的方法,用它做一些事情,然后修改一个“返回”对象,其中包含返回到浏览器的内容。对于 web.py,这是 web.input() - 请参阅此处

对于更复杂的事情,您基本上需要以您选择的格式 POST 数据 - XML、JSON、神奇的二进制 blob 格式。你如何解析它取决于它是什么。只需 Google 搜索“python xml”或“python json”或其他内容,您就会找到它的最新库。

插入数据库 - 使用 Python 的 mysqldb 库(我使用 Postgres,主要来自 Ruby 和 Java,所以我不太熟悉最新的 Python MySQL 库)。

对于发送电子邮件,您可以只使用 sendmail (在安装了 sendmail 的 Unix 系统上),也可以使用 Python 的 smtplib 将其发送到 SMTP 服务器 - 如果您只是发送管理电子邮件,请使用 Gmail 之类的东西帐户作为您的 SMTP 服务器,因为您知道它会正常工作。哦,web.py 让这一切变得简单 - 它有一个内置的邮件模块。我猜用那个吧。

您可能需要考虑安全性 - 这意味着身份验证和表单验证。 RTFM 适用于您使用的任何语言和数据库库,这样您就不会遭受 SQL 注入 - 对电子邮件也是如此。您不想成为垃圾邮件的代理。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文