将Python文件的执行限制在服务器上(禁止从浏览器访问)

2024-03-28 17:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个Python文件,我想阻止任何人执行,除非它来自服务器本身。这些文件实现了一个功能,可以增加用户的资金量。我需要的是使这个文件不在web上公开,这样如果有人试图调用这个文件,该文件就会拒绝这个请求,除非调用来自服务器。在

有人知道我怎么做吗?我的第一个想法是检查他们的IP地址。在

示例

假设我有这个文件:function.py,这个文件中的一个函数将接受一个新的金额,并增加数据库中适当的余额。在

当有人试图将数据发布到这个文件中,而这个人在服务器之外(比如从244.23.23.0)这个文件将是可访问的。但是,可以从服务器本身调用函数。在

因此文件可以访问服务器上的其他文件,但外部用户不能访问,结果是除非从服务器调用此文件,否则没有人可以执行该文件。

这对我来说真的很重要,因为它关系到真正的金钱。另外,这笔钱将来自PayPal IPN。事实上,如果有一种方法可以阻止访问,除非它来自PayPal,那将是一个很棒的安全应用程序的方法。在

好吧,就我所尝试的:

  1. 使用Google将数据库放到云SQL中[https://developers.google.com/cloud-sql/]在
  2. 尝试检查文件中传入请求的IP

谢谢你的帮助。在


Tags: 文件方法函数用户py功能服务器web
3条回答

关于贝宝。。

我建议你看看paypal的this example,它们是x.com的一部分,这是合法的。

重要的是:

// STEP 2: Post IPN data back to paypal to validate

$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

// In wamp like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
    // error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);


// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment

    // assign posted variables to local variables
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];
} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
}

你可以在paypal把数据发送回paypal,然后把你需要的数据发送回paypal,然后你就可以把数据发送到paypal了。

.htaccess、chmod或您可以使用自己定义的密钥。。。你有几种可能性。

编辑:无论如何,如果文件只包含函数。任何人都不能从外部http请求使用它,除非您在这个文件中实际调用它:function();

如果使用Apache,则可以使用.htaccess限制对文件的访问:

http://httpd.apache.org/docs/current/howto/htaccess.html

相关问题 更多 >