forked from OA-PASS/pass-http-cgi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgi_server.py
57 lines (48 loc) · 2.03 KB
/
cgi_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# A simple HTTP server that responds to POSTs by running a shell command
# Usage: python cgi_server.py &
import os
import BaseHTTPServer
from subprocess import call
# Edit these values to control the server behavior
PORT = int(os.getenv('PY_CGI_PORT', "8080"))
DEBUG_PORT = os.getenv('FTP_SUBMISSION_DEBUG_PORT', "5005")
FTP_CONFIGURATION_KEY = os.getenv('FTP_CONFIGURATION_KEY', "local")
COMMAND = "java"
ARGS = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=" + DEBUG_PORT,
"-jar",
"/usr/local/src/nihms-submission/nihms-cli/target/nihms-cli-1.0.0-SNAPSHOT-shaded.jar",
"/usr/local/src/nihms-submission/nihms-cli/src/main/resources/FilesystemModelBuilderTest.properties",
FTP_CONFIGURATION_KEY
]
# Define a request handler class that runs a command when it receives a POST
class RequestHandler (BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(s):
# Execute the command and prepare the POST response code.
# Prints an error message to the console on failure.
# This is a blocking function. Use subprocess.Popen for non-blocking.
try:
status = call([COMMAND]+list(ARGS))
if status != 0:
args = " ".join(str(x) for x in ARGS)
print "Command '{} {}' failed with code {}".format(COMMAND, args, status)
code = 500
else:
code = 200
except:
args = " ".join(str(x) for x in ARGS)
print "Command '{} {}' failed with an exception".format(COMMAND, args)
code = 500
# Prepare and send the response header
s.send_response(code)
s.send_header("Content-type", "text/html")
s.send_header("Access-Control-Allow-Origin", "*")
s.end_headers()
# Create and run the server
if __name__ == "__main__":
httpd = BaseHTTPServer.HTTPServer(("", PORT), RequestHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()