I am trying to run a python script (adsb-exchange.py) on an Apache httpd webserver running on an EC2 instance. The script runs perfect from the AWS CLI, but when I try and run from a simple html page off the server I get:
Internal Server Error 500 - The server encountered an internal error or misconfiguration and was unable to complete your request.
Note, running a simple 'hello.py' script from the html page works fine.
Everything is added under the /var/www/html directory and the httpd.conf file is updated to reflect the DocumentRoot path.
Index.html file:
<h1>Welcome!</h1>
<p>Click the button below to start running the scipt...</p>
<form action="/adsb-exchange.py" method="post">
<button>Start</button>
</form>
adsb-exchange.py file:
#!/usr/bin/python
#!/usr/bin/env python
#import Python modules
import cgi
import sys
cgi.enable()
print "Content-type: text/html\n\n"
print
#application imports
import modules.get_config_vals as cfgvals
import modules.adsb_pull as ap
import modules.json2csv as j2c
#get payload params from adsb_config file
payload = cfgvals.get_payload()
#get the JSON pull frequency and runtime from config file
adsb_freq = cfgvals.get_pullfreq()
#send to adsb_pull module, create a JSON object file, and get the filename returned
f = ap.pull_data(payload, adsb_freq)
#convert JSON object file to CSV, output CSV file to directory
j2c.j2csv(f)
raw_input("Press Enter to exit")
The adsb-exchange.py file makes calls to various modules under the 'modules' folder via the /var/www/html/modules path, and each module includes the lines:
print "Content-type: text/html\n\n"
print
GET and POST methods do not appear to impact the results. Output files are dumped to the same directory as adsb-exchange.py is in, for now.
Again, the script runs when executed directly from the ACL, but not when it is called from the index.html page.
Thanks!
Related
I'm working through Programming Python and am trying to work through the basic CGI example. I so far have the following files saved in the same directory (titled: cgi101.html and webserver.py respectively):
<html>
<title>Interactive Page</title>
<body>
<form method=POST action="cgi-bin/cgi101.py">
<p><b>Enter your name:</b>
<p><input type=text name=user>
<p><input type=submit>
</form>
</body>
import os, sys
from http.server import HTTPServer, CGIHTTPRequestHandler
webdir = '.'
port = 8080
os.chdir(webdir)
srvraddr = ("", port)
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever()
Then in a sub directory titled cgi-bin I have the following script called cgi101.py saved:
#!/usr/bin/python
import cgi
form = cgi.FieldStorage()
print('Content-type: text/html\n')
print('<title>Reply Page</title>')
if not 'user' in form:
print('<h1>Who are you?</h1>')
else:
print('<h1>Hello <i>%s</i>!</h1>' % cgi.escape(form['user'].value))
I then follow these steps:
Run webserver.py from the directory where webserver.py and cgi101.html are located
Navigate to http://localhost:8080/cgi101.html this opens up as expected
When I enter a name and submit, initially I get an error saying the cgi script is not executable. If I go to cgi101.py and then run chmod 744 to make it executable, when I repeat the above steps the following URL renders: http://localhost:8080/cgi-bin/cgi101.py but no content renders whatsoever and the command line where I ran the server gives a FileNotFound error.
What am I doing wrong?
Any help appreciated.
Richard
I am trying to run multiple Bokeh servers in the flask app , the plots function correctly on their own using method like this:
def getTimePlot():
script = server_document('http://localhost:5006/timeseries')
return render_template("displaytimeseries.html", script=script, template="Flask")
def startPlotserver():
server.start()
server = Server({'/timeseries': modifyTimeSeries}, io_loop=IOLoop(), allow_websocket_origin=["localhost:8000"])
server.io_loop.start()
if __name__ == '__main__':
print('Opening single process Flask app with embedded Bokeh application on http://localhost:8000/')
print()
print('Multiple connections may block the Bokeh app in this configuration!')
print('See "flask_gunicorn_embed.py" for one way to run multi-process')
app.run(port=5000, debug=True)
but when i try to embed two servers together to flask using this approach that's where i get the problems:
file structure:
|--app4
|---webapp2.py
|---bokeh
|--timeseries.py
|--map.py
I think i have found the workaround in here Link To Question
I am have trying now to import the map server to flak using the similar method mentioned and ended up with something like this:
1. File builder (not sure why it's not picking it up)
def build_single_handler_applications(paths, argvs=None):
applications = {}
argvs = {} or argvs
for path in paths:
application = build_single_handler_application(path, argvs.get(path, []))
route = application.handlers[0].url_path()
if not route:
if '/' in applications:
raise RuntimeError("Don't know the URL path to use for %s" % (path))
route = '/'
applications[route] = application
return applications
2. Code to find file and create connection
files=[]
for file in os.listdir("bokeh"):
if file.endswith('.py'):
file="map"+file
files.append(file)
argvs = {}
urls = []
for i in files:
argvs[i] = None
urls.append(i.split('\\')[-1].split('.')[0])
host = 'http://localhost:5006/map'
apps = build_single_handler_applications(files, argvs)
bokeh_tornado = BokehTornado(apps, extra_websocket_origins=["localhost:8000"])
bokeh_http = HTTPServer(bokeh_tornado)
sockets, port = bind_sockets("localhost:8000", 5000)
bokeh_http.add_sockets(sockets)
3. Code which calls the server and renders template
#app.route('/crimeMap', methods=['GET'])
def getCrimeMap():
bokeh_script = server_document("http://localhost:5006:%d/map" % port)
return render_template("displaymap1.html", bokeh_script=bokeh_script)
i am running both of my Bokeh servers in single command like this
bokeh serve timeseries.py map.py --allow-websocket-origin=127.0.0.1:5000
but when i run webapp2.py i am getting this error:
(env1) C:\Users\Dell1525\Desktop\flaskapp\env1\app4>webapp2.py
Traceback (most recent call last):
File "C:\Users\Dell1525\Desktop\flaskapp\env1\app4\webapp2.py", line 113, in <module>
apps = build_single_handler_applications(files, argvs)
File "C:\Users\Dell1525\Desktop\flaskapp\env1\app4\webapp2.py", line 29, in build_single_handler_applications
application = build_single_handler_application(path, argvs.get(path, []))
NameError: name 'build_single_handler_application' is not defined
i found and added build_single_handler_application function from Bokeh docs only because of this error so i am not sure if it was even required or is correct. I am wondering what am i missing to make this work just in case it is positioning error or missing imports i am attaching full flask webapp2.py code here :
Full Code
Thak you so much for help
i have found an easier solution by tweaking this example a little: Link To Original Post Note this requires you to have tornado 4.4.1 as its not working with newer versions
trick is to run all servers individually and on different ports with same socket access like this
bokeh serve timeseries.py --port 5100 --allow-websocket-origin=localhost:5567
bokeh serve map.py --port 5200 --allow-websocket-origin=localhost:5567
for those who might find this useful i have included full working solution Link To Working Code
I have been searching since a couple of days for a solution without success.
We have a windows service build to copy some files from one location to another one.
So I build the code shown below with Python 3.7.
The full coding can be found on Github.
When I run the service using python all is working fine, I can install the service and also start the service.
This using commands:
Install the service:
python jis53_backup.py install
Run the service:
python jis53_backup.py start
When I now compile this code using pyinstaller with command:
pyinstaller -F --hidden-import=win32timezone jis53_backup.py
After the exe is created, I can install the service but when trying to start the service I get the error:
Error starting service: The service did not respond to the start or
control request in a timely fashion
I have gone through multiple posts on Stackoverflow and on Google related to this error however, without success. I don't have the option to install the python 3.7 programs on the PC's that would need to run this service. That's why we are trying to get a .exe build.
I have made sure to have the path updated according to the information that I found in the different questions.
Image of path definitions:
I also copied the pywintypes37.dll file.
From -> Python37\Lib\site-packages\pywin32_system32
To -> Python37\Lib\site-packages\win32
Does anyone have any other suggestions on how to get this working?
'''
Windows service to copy a file from one location to another
at a certain interval.
'''
import sys
import time
from distutils.dir_util import copy_tree
import servicemanager
import win32serviceutil
import win32service
from HelperModules.CheckFileExistance import check_folder_exists, create_folder
from HelperModules.ReadConfig import (check_config_file_exists,
create_config_file, read_config_file)
from ServiceBaseClass.SMWinService import SMWinservice
sys.path += ['filecopy_service/ServiceBaseClass',
'filecopy_service/HelperModules']
class Jis53Backup(SMWinservice):
_svc_name_ = "Jis53Backup"
_svc_display_name_ = "JIS53 backup copy"
_svc_description_ = "Service to copy files from server to local drive"
def start(self):
self.conf = read_config_file()
if not check_folder_exists(self.conf['dest']):
create_folder(self.conf['dest'])
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
while self.isrunning:
# Copy the files from the server to a local folder
# TODO: build function to trigger only when a file is changed.
copy_tree(self.conf['origin'], self.conf['dest'], update=1)
time.sleep(30)
if __name__ == '__main__':
if sys.argv[1] == 'install':
if not check_config_file_exists():
create_config_file()
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(Jis53Backup)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(Jis53Backup)
I was also facing this issue after compiling using pyinstaller. For me, the issue was that I was using the paths to configs and logs file in dynamic way, for ex:
curr_path = os.path.dirname(os.path.abspath(__file__))
configs_path = os.path.join(curr_path, 'configs', 'app_config.json')
opc_configs_path = os.path.join(curr_path, 'configs', 'opc.json')
log_file_path = os.path.join(curr_path, 'logs', 'application.log')
This was working fine when I was starting the service using python service.py install/start. But after compiling it using pyinstaller, it always gave me error of not starting in timely fashion.
To resolve this, I made all the dynamic paths to static, for ex:
configs_path = 'C:\\Program Files (x86)\\ScantechOPC\\configs\\app_config.json'
opc_configs_path = 'C:\\Program Files (x86)\\ScantechOPC\\configs\\opc.json'
debug_file = 'C:\\Program Files (x86)\\ScantechOPC\\logs\\application.log'
After compiling via pyinstaller, it is now working fine without any error. Looks like when we do dynamic path, it doesn't get the actual path to files and thus it gives error.
Hope this solves your problem too. Thanks
I'm doing a testing unit that requires the tests to be run via web browser. I'm using Ubuntu VPS 14 with LAMP stack installed, mod_wsgi, selenium 2.44 and PhantomJS 1.9. I'm testing with the very simple code first:
from flask import Flask, request
from selenium import webdriver
app = Flask(__name__)
app.debug = True
#app.route("/test")
def test():
url = "http://www.google.com"
driver = webdriver.PhantomJS('./phantomjs')
driver.get(url)
result = driver.page_source
driver.close()
return result
if __name__ == "__main__":
app.run()
The code runs very smoothly on my local Ubuntu, it prints out the google page when I connect to: 127.0.0.1:5000/test . On my Ubuntu VPS, my have my flask already setup and running. Now i use the same code to be the index file (supposed all configs are OK and 'hello world' runs), I have 500 Internal Server Error when connecting to http://xxx.xxx.xxx.xxx/test
Apache log sends out the following error:
... service_args=service_args, log_path=service_log_path File
"/usr/local/lib/python2.7/ddist-packages/selenium/webdriver/phantomjs/service.py",
line 53, in init
self._log = open(log_path, 'w') in > ignored Exception AttributeError: "'Service' object
has no attribute '_log'" in > ignored
I changed the log_path for phatomJS but still have the same problem. However, if I open python console, doing line by line as following:
from selenium import webdriver
br = webdriver.PhantomJS('./phantomjs')
....
I got no error. It took me the whole day to fin the problem but I could't be able to fix it. Any ideas how to solve this problem?
Figure out the problem, current phantomjs and init.py don't have enough permission to manipulate the service.py of ghostdriver. Here is the fix:
Add a new user: "add username", then set "add username sudo"
Login to the new user, make sure every command you run from now on starts with "sudo"
In your flask application where init.py resides, create "ghostdriver.log" and "phantomjs" which is an executable file.
Chmod both of them to 777 : init.py, ghostdriver.log and phantomjs
Set the custom config in init.py for phantomjs:
br = webdriver.PhantomJS(service_log_path='./ghostdriver.log', executable_path='./phantomjs')
That's it, now your selenium + flask + phantomjs is now working correctly.
I wanted to create a simple app using webapp2. Because I have Google App Engine installed, and I want to use it outside of GAE, I followed the instructions on this page: http://webapp-improved.appspot.com/tutorials/quickstart.nogae.html
This all went well, my main.py is running, it is handling requests correctly. However, I can't access resources directly.
http://localhost:8080/myimage.jpg or http://localhost:8080/mydata.json
always returns a 404 resource not found page.
It doesn't matter if I put the resources on the WebServer/Documents/ or in the folder where the virtualenv is active.
Please help! :-)
(I am on a Mac 10.6 with Python 2.7)
(Adapted from this question)
Looks like webapp2 doesn't have a static file handler; you'll have to roll your own. Here's a simple one:
import mimetypes
class StaticFileHandler(webapp2.RequestHandler):
def get(self, path):
# edit the next line to change the static files directory
abs_path = os.path.join(os.path.dirname(__file__), path)
try:
f = open(abs_path, 'r')
self.response.headers.add_header('Content-Type', mimetypes.guess_type(abs_path)[0])
self.response.out.write(f.read())
f.close()
except IOError: # file doesn't exist
self.response.set_status(404)
And in your app object, add a route for StaticFileHandler:
app = webapp2.WSGIApplication([('/', MainHandler), # or whatever it's called
(r'/static/(.+)', StaticFileHandler), # add this
# other routes
])
Now http://localhost:8080/static/mydata.json (say) will load mydata.json.
Keep in mind that this code is a potential security risk: It allows any visitors to your website to read everything in your static directory. For this reason, you should keep all your static files to a directory that doesn't contain anything you'd like to restrict access to (e.g. the source code).