Python不调用外部程序第3部分 [英] Python not calling an external program part 3

查看:80
本文介绍了Python不调用外部程序第3部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从python程序运行外部程序时遇到问题,该程序是由postgres 9.2数据库中的触发器生成的。触发器起作用。它写入文件。我曾尝试仅运行外部程序,但权限不允许其运行。我能够创建一个文件夹(使用os.system( mkdir))。该文件夹的所有者是NETWORK SERVICE。

I have been having problems trying to run an external program from a python program that was generated from a trigger in a postgres 9.2 database. The trigger works. It writes to a file. I had tried just running the external program but the permissions would not allow it to run. I was able to create a folder (using os.system("mkdir") ). The owner of the folder is NETWORK SERVICE.

我需要运行一个名为sdktest的程序。当我尝试运行它时,没有响应发生,因此我认为这意味着python程序没有足够的权限(具有NETWORK SERVICE的所有者)来运行它。

I need to run a program called sdktest. When I try to run it no response happens so I think that means that the python program does not have enough permissions (with an owner of NETWORK SERVICE) to run it.

我一直在将程序需要的文件复制到该目录中,以便它们具有正确的权限,并且在某种程度上可以正常工作,但是我需要运行的程序是最后一个,并且由于没有足够的权限。

I have been having my program copy files that it needs into that directory so they would have the correct permissions and that has worked to some degree but the program that I need to run is the last one and it is not running because it does not have enough permissions.

我的python程序运行一个名为PG_QB_Connector的C ++程序,该程序调用sdktest。

My python program runs a C++ program called PG_QB_Connector which calls sdktest.

有什么方法可以将流程的所有者更改为正常所有者吗?有一个更好的方法吗?基本上,我只需要让此C ++程序具有足够的烫发才能正确运行。

Is there any way I can change the owner of the process to be a "normal" owner? Is there a better way to do this? Basically I just need to have this C++ program have eniough perms to run correctly.

BTW,当我手动运行C ++程序时,运行sdktest程序的行将运行正确,但是,当我从postgres / python运行它时,它什么也没做...

BTW, when I run the C++ program by hand, the line that runs the sdktest program runs correctly, however, when I run it from the postgres/python it does not do anything...

我有Windows 7,python 3.2。我对此提出的其他两个问题位于此处此处

I have Windows 7, python 3.2. The other 2 questions that I asked about this are located here and here

python程序:

CREATE or replace FUNCTION scalesmyone (thename text)
RETURNS int
AS $$
a=5
f = open('C:\\JUNK\\frompython.txt','w')
f.write(thename)
f.close()
import os
os.system('"mkdir C:\\TEMPWITHOWNER"')
os.system('"mkdir C:\\TEMPWITHOWNER\\addcustomer"')
os.system('"copy  C:\\JUNK\\junk.txt C:\\TEMPWITHOWNER\\addcustomer"')
os.system('"copy  C:\\BATfiles\\junk6.txt   C:\\TEMPWITHOWNER\\addcustomer"')
os.system('"copy  C:\\BATfiles\\run_addcust.bat   C:\\TEMPWITHOWNER\\addcustomer"')
os.system('"copy  C:\\Workfiles\\PG_QB_Connector.exe  C:\\TEMPWITHOWNER\\addcustomer"')
os.system('"copy  C:\\Workfiles\\sdktest.exe  C:\\TEMPWITHOWNER\\addcustomer"')
import subprocess
return_code = subprocess.call(["C:\\TEMPWITHOWNER\\addcustomer\\PG_QB_Connector.exe", '"hello"'])
$$ LANGUAGE plpython3u;

下面是从python程序调用并调用sdktest.exe的C ++程序

The C++ program that is called from the python program and calls sdktest.exe is below

command = "copy C:\\Workfiles\\AddCustomerFROMWEB.xml C:\\TEMPWITHOWNER\\addcustomer\\AddCustomerFROMWEB.xml";
system(command.c_str());


//everything  except for the qb file is in my local folder
command = "C:\\TEMPWITHOWNER\\addcustomer\\sdktest.exe  \"C:\\Users\\Public\\Documents\\Intuit\\QuickBooks\\Company Files\\Shain Software.qbw\"  C:\\TEMPWITHOWNER\\addcustomer\\AddCustomerFROMWEB.xml C:\\TEMPWITHOWNER\\addcustomer\\outputfromsdktestofaddcust.xml";
system(command.c_str());


推荐答案

听起来好像您想调用命令行

It sounds like you want to invoke a command-line program from within a PostgreSQL trigger or function.

通常更好的选择是让触发器发送 NOTIFY 并具有PostgreSQL连接 LISTEN 的进程以接收通知。收到通知时,该过程可以启动您的程序。这是我建议的方法;它更清洁,这意味着您的程序不必在PostgreSQL的用户ID下运行。参见 NOTIFY LISTEN

A usually-better alternative is to have the trigger send a NOTIFY and have a process with a PostgreSQL connection LISTENing for notifications. When a notification comes in, the process can start your program. This is the approach I would recommend; it's a lot cleaner and it means your program doesn't have to run under PostgreSQL's user ID. See NOTIFY and LISTEN.

如果您确实需要从Pg内部运行命令:

If you really need to run commands from inside Pg:

您可以使用< a href = http://www.postgresql.org/docs/current/static/plpython.html rel = nofollow> PL / Pythonu 使用 os.system subprocess.check_call PL / Perlu 使用 system();等等。如果需要,所有这些都可以从Pg内部运行命令。您无法直接从PostgreSQL调用程序,需要使用一种不受信任(意味着完全特权,而不是沙盒化)的程序语言来调用外部可执行文件。 PL / TCL也可以做到这一点。

You can use PL/Pythonu with os.system or subprocess.check_call; PL/Perlu with system(); etc. All these can run commands from inside Pg if you need to. You can't invoke programs directly from PostgreSQL, you need to use one of the 'untrusted' (meaning fully privileged, not sandboxed) procedural languages to invoke external executables. PL/TCL can probably do it too.

更新

您的Python上面显示的代码有几个问题:

Your Python code as shown above has several problems:


  • 在Python中使用 os.system 复制文件是错误的。使用 shutil 库: http:/ /docs.python.org/3/library/shutil.html 复制文件,并使用简单的 os.mkdir 命令创建目录。

  • 双层报价似乎不正确;您不是要只引用每个参数而不引用整个命令吗?无论如何,您应该使用 subprocess.call 而不是 os.system

  • 您的最终 subprocess.call 调用看起来不错,但是无法检查错误代码,因此您永远不会知道它是否出错;您应该改用 subprocess.check_call

  • Using os.system in Python to copy files is just wrong. Use the shutil library: http://docs.python.org/3/library/shutil.html to copy files, and the simple os.mkdir command to create directories.
  • The double-layered quoting looks wrong; didn't you mean to quote only each argument not the whole command? You should be using subprocess.call instead of os.system anyway.
  • Your final subprocess.call invocation appears OK, but fails to check the error code so you'll never know if it went wrong; you should use subprocess.check_call instead.

C ++代码也似乎无法检查 system()调用中的错误,因此您永远不会知道它运行的命令是否失败。

The C++ code also appears to fail to check for errors from the system() invocations so you'll never know if the command it runs fails.

像Python代码一样,使用 copy shell命令在C ++中复制文件通常是错误的。 Microsoft Windows提供了 CopyFile 函数;等价物或替代品存在于其他平台上,您也可以使用便携式但效率较低的流复制。

Like the Python code, copying files in C++ by using the copy shell command is generally wrong. Microsoft Windows provides the CopyFile function for this; equivalents or alternatives exist on other platforms and you can use portable-but-less-efficient stream copying too.

这篇关于Python不调用外部程序第3部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆