为了允许帮助台重新启动Oracle实例,我们正在尝试实现一个小型python we服务器,该服务器将启动一个启动Oracle实例的shell脚本。
代码已经完成并启动了实例,但是有一个问题:实例连接到been服务器,因此浏览器的缓冲区直到实例停止后才会关闭,并且有一个ora_pmon_INSTANCE进程在been服务器端口上进行侦听。
我尝试使用以下命令启动脚本:
process = os.system("/home/oracle/scripts/webservice/prueba.sh TFINAN")和
process = subprocess.Popen(["/home/oracle/scripts/webservice/prueba.sh", "TFINAN"], shell=False, stdout=subprocess.PIPE)`但这一切都是一样的。
我还尝试使用daemon启动一个脚本(使用redhat的init脚本中的daemon函数)。该脚本启动Oracle实例,并显示相同的结果。
这是我的代码:
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn
import threading
import argparse, urlparse
import re
import cgi, sys, time
import os, subprocess
class HTTPRequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
self.send_response(403)
self.send_header('Content-Type', 'text/html')
self.end_headers()
return
def do_GET(self):
ko = False
respuesta = ""
params = {}
myProc = -1
parsed_path = urlparse.urlparse(self.path)
try:
params = dict([p.split('=') for p in parsed_path[4].split('&')])
except:
params = {}
elif None != re.search('/prueba/*', self.path):
self.send_response(200)
respuesta = "Hola Mundo! -->" + str( params['database'] )
elif None != re.search('/startup/*', self.path):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
cmd = """ <html>
<body><H2> Iniciando instancia oracle: """ + str( params["database"]) + '. Espere un momento, por favor ...</H2>'
self.wfile.write(cmd)
#process = os.system("/home/oracle/scripts/webservice/prueba.sh INSTANCE")
process = subprocess.Popen(["/home/oracle/scripts/webservice/prueba.sh", "INSTANCE"], shell=False, stdout=subprocess.PIPE)
# wait for the process to terminate
out, err = process.communicate()
errcode = process.returncode
if errcode == 0:
self.wfile.write("""<H1> Instancia iniciada correctamente
</H1>
</body> </html>""")
self.wfile.close()
else:
respuestaok = "Error inicializando la instancia: " + str( params['database']) + " Intentelo de nuevo pasados unos minutos y si vuelve a fallar escale la incidencia al siguiente nivel de soporte"
else:
self.send_response(403, 'Bad Request: pagina no existe')
respuesta = "Solicitud no autorizada"
if respuesta != "":
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(respuesta)
self.wfile.close()
if ko:
server.stop()
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
allow_reuse_address = True
def shutdown(self):
self.socket.close()
sys.exit(0)
class SimpleHttpServer(object):
def __init__(self, ip, port):
self.server = ThreadedHTTPServer((ip,port), HTTPRequestHandler)
def start(self):
self.server_thread = threading.Thread(target=self.server.serve_forever)
self.server_thread.daemon = True
self.server_thread.start()
def waitForThread(self):
self.server_thread.join()
def stop(self):
self.server.shutdown()
if __name__=='__main__':
parser = argparse.ArgumentParser(description='HTTP Server')
parser.add_argument('port', type=int, help='Listening port for HTTP Server')
parser.add_argument('ip', help='HTTP Server IP')
args = parser.parse_args()
server = SimpleHttpServer(args.ip, args.port)
print 'HTTP Server Running...........'
server.start()
server.waitForThread()你们有人能帮我吗?
发布于 2014-04-18 04:00:34
您的问题与HTTP服务器没有太多关系。从Python代码控制Oracle守护进程似乎存在一般问题。
首先尝试编写一个简单的python脚本,它可以完成您所需的工作。
我的猜测是,您的尝试在读取守护程序控制脚本的输出时出现了问题。
有关读取命令输出的信息,另请参阅Popen.communicate()。另一种选择是调用subrocess.call()
有许多从Python调用系统命令的教程,例如this one
除了纯粹与Python相关的问题,您可能会遇到权限问题-如果您的用户,运行脚本/HTTP服务器不允许调用oracle控制脚本,您有另一个问题(它可能有一个解决方案,在Linux上将该用户添加到sudoers中)。
在解决了调用脚本的问题之后,应该很容易让它在您的HTTP服务器中工作。
https://stackoverflow.com/questions/23113840
复制相似问题