I'm super new to web development, but not new to programming as a whole. I'm extremely confused by this application which I was pulled into fixing because the person who normally does it can't anymore.
This may be a silly question, but I can't find the code or any HTML that contains the actual web pages. In the main folder, ~/myapp, there contains:
- README.txt
- build
- env
- bin
- include
- lib
- local
- man
- passenger_wsgi.py
- passenger_wsgi.pyc
- pip-selfcheck.json
- public
- favicon.gif
- favicon.ico
- quickstart.html
- sd.html
- tmp
- error.log
- favicon.gif
- favicon.ico
- get-pip.py
- phpinfo.php
- pylibs
- quickstart.html
- sd.log
- myapp_flask
Observations:
env/bin/, env/include/, and env/lib/ all have what appear to be versions of python in them.
env/local/ contains its own bin/, include/, and lib/ directories that contain the exact same thing as the others.
tmp contains the standard restart.txt used for Apache rails applications. (Note: touching this does restart the server, so this env/ directory is linked to the application itself.).
Questions:
Does anyone recognize this file structure? What application type is it? Php? Rails? Something else?
Does anyone know where I could look for the code that contains the actual web page? This is a full-fledged, live webpage that people have been using for a while, but I can't find the html (not even for the homepage). I've been searching for days, so I know I'm going about this wrong.
Additional notes:
This error occurs when I restart the web application with touch tmp/restart.txt/
Web application could not be started
An error occurred while starting the web application. It exited before signalling successful startup back to Phusion Passenger. Please read this article for more information about this problem.
Raw process output:
Traceback (most recent call last):
File "/usr/share/passenger/helper-scripts/wsgi-loader.py", line 26, in <module>
import sys, os, re, imp, threading, signal, traceback, socket, select, struct, logging, errno
File "/usr/lib/python2.7/logging/__init__.py", line 26, in <module>
import sys, os, time, cStringIO, traceback, warnings, weakref, collections
File "/usr/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
both quickstart.html contents are identical:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>DreamHost</title>
<link media="all" rel="stylesheet" type="text/css" href="http://dreamhost.com/wp-content/themes/dreamhost/style.css" />
</head>
<body id="domains">
<div class="w1">
<div class="w2">
<div id="wrapper">
<div id="header">
<strong class="comingsoon-logo">DreamHost Imagine the Web, Your Way</strong>
</div>
<div class="main" style="padding-bottom: 24px">
<h1>sendsomething.net Coming Soon!</h1>
<div class="holder">
<div class="content">
<p>The DreamHost customer who owns sendsomething.net has not yet uploaded their website or has chosen to leave this holding page active.</p>
<p>If you are the owner of this domain, you'll find your login information contained within the emails sent to you when your account was activated. Once logged in, you'll be able to delete this page (quickstart.html) and upload your new site.</p>
<p>Here are some helpful links for getting started:</p>
<p>
<img src="http://images.dreamhost.com/btn-orange-webpanel.png" alt="DreamHost Web Panel" title="DreamHost Web Panel" />
<img src="http://images.dreamhost.com/btn-blue-wiki.png" alt="DreamHost Wiki" title="DreamHost Wiki" />
<img src="http://images.dreamhost.com/btn-green-discussion.png" alt="DreamHost Discussion Forum" title="DreamHost Discussion Forum" />
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
Thank you for your time and assistance!
Edit:
Per request of answerer, Here are the contents of ~/myapp/myapp_flask/:
- .hg
- .hgignore
- backup_config.ini
- backup_db
- db_backups
- db_repo
- dbmanage.py
- doc
- dump.sql
- example_backup_config.ini
- instance
- migrate_dbs.py
- new.db
- new_bkp.db
- requirements.txt
- restored.db
- restored.dbe
- myapp
- __init__.py
- __init__.py.orig
- __init__.pyc
- app.py
- app.pyc
- forms.py
- forms.pyc
- helpers.py
- helpers.pyc
- static
- templates
- user.py
- user.pyc
- views.py
- views.pyc
- myapp.wsgi
- teste.db
- webfaction.db
Note: This might not be a complete answer, but it might be a starting place for one.
Possibly in answer to your first question, and based on the comments from earlier, this project looks to me like it might be a Flask web application. Flask is a Python microframework that can be used to build simple or complex web applications. I'm guessing this based on the following observations:
The passenger_wsgi.py file
The observation of the files ending in .py made by brombeer in the comments
The item at the bottom of your application structure list called myapp_flask
As far as the passenger_wsgi.py file, Passenger is a web application server used to run web applications written in things like Python, Ruby, or Node.js.
As far as brombeer's observation about the file extensions, files that end in .py are typically Python files.
As far as that myapp_flask item, if there are contents inside this file or folder, I wonder if it might contain the main web application code. Would it be possible to check that file or folder to see if it contains application code? Warning: It might contain sensitive data (e.g., passwords, secrets, or keys), so I don't know if it should be posted in your original post above.
So, I guess I touched on your second main question in that earlier response: you might be able to find the main web application code in the myapp_flask item. If nothing is there, however, you might be able to determine where the main application code is by looking at the contents of your passenger_wsgi.py.
In this example passenger_wsgi.py file in the Passenger documentation, it shows how an example (Django, in that case) application might be referred to in this type of file. If you open your passenger_wsgi.py in a text editor, it might a show a reference to the main application code on a line that contains the term application = (or something similar), like in the earlier example. Maybe this could help you know what the name of the application might be.
Then, you could possibly use grep to search for the application name in your file structure, if grep is available on your system. Maybe using a search command like: grep -r 'application-name' *. That might give you more clues as to where the main application code is located.
As far as the additional note about your ImportError: cannot import name _remove_dead_weakref error, I don't know if it's similar to this other question from the past. I would recommend caution, however, before changing up your Python 2 related setup based on answers in that other question thread, unless you are alright with whatever might happen along the way. I haven't tried out the answers myself from that thread.
Related
This question already has answers here:
Flask blueprint static directory does not work?
(6 answers)
Closed 4 years ago.
I'm working on creating a personal test-website for myself using Flask, however I discovered a behaviour I can not full get behind and could need some help with.
I have separated my website into indivudial chunks using Flask's blueprint system, because it makes sense for my case (since I want it to contain multiple smaller test-applications). I am suspecting that my problem is rooted in my project stucture, so I am giving a brief overview over what I did. Here is my (simplified) project setup:
>File structure:
root (contains some linux start scripts)
- run.py
- website (the actual flask project folder)
- __init__.py (registers blueprints)
- blueprints
- __init__.py (empty)
- website
- __init__.py (defines routes, creates blueprint)
- static (static files for this blueprint)
- css
- example.css
- templates (render templates for this blueprint)
- example.html.j2
- app1
- <Same structure as above>
- app2
- <Same structure as above>
- ...
>run.py
from website import createApp
createApp().run(debug=True)
>website/__init__.py:
from flask import Flask, render_template
def createApp():
app = Flask(__name__)
app.testing = True
# Website
from blueprints.website import website
app.register_blueprint(website())
# App1
from blueprints.app1 import app1
app.register_blueprint(app1())
# App2
from blueprints.app2 import app2
app.register_blueprint(app2())
...
return app
>website/blueprints/website/__init__.py:
from flask import Blueprint, render_template
bp = Blueprint("website", __name__, url_prefix="/",
template_folder="templates", static_folder="static")
def website():
return bp
#bp.route('/')
def index():
return render_template('example.html.j2')
>website/blueprints/website/templates/example.html.j2
<html>
<head>
<link rel="stylesheet", href="{{url_for('website.static', filename='css/example.css')}}">
<title>Test Page!</title>
</head>
<body>
This is a test page!
</body>
</html>
Expected result: The page should appear with the style defined in example.css
Actual result: Loading the example.css document results in a 404 error.
Since I've tried to deal with this for a few hours now I think I have nailed the problem down to Flask being weird when it comes to the root address.
Since the blueprint defines the address as url_prefix="/" I access it by typing "website.com" into my browser. (The browser tries to call the resource over "website.com/static/css/example.css", but get a 404 response.)
If I change the address to something like url_prefix="/test" and access the page via "website.com/test", the stylesheet will be loaded successfully. (The browser now tries to call the resource over "website.com/test/static/css/example.css", this time the document is found and loaded.)
Since this is supposed to be the main page I do want for it to use the root address though.
I would be grateful for someone to shine some light onto this and explain to me where my error lays.
Interesting question. The only thing I can think off is that you might have specified that website.com/static holds all your static files in your WSGI server script. Therefore, the flask app doesn't interfere which requests to website.com/static and these requests are handled by the WSGI server which can't find them in the folder.
Does this problem also occur when using the development server?
Can you try changing the static server in your WSGI setup to the website/blueprints/static/website folder?
And finally if this doesn't help, can you make a small github repo with this problem? Its pretty hard to reproduce this problem with these kinds of imports and filetrees.
This question already has answers here:
How to serve static files in Flask
(24 answers)
Link to Flask static files with url_for
(2 answers)
Closed 4 years ago.
I'm pretty new to python, even less experienced with flask, and I cannot figure out this issue. I have the following simple web page with jQuery functionality that works great when I double click the file and open it in a browser:
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.3.1.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
$("#updateBtn").on("click", function() {
text = "<h2>The div has been updated!</h2>";
$("#jQuery_div").html(text);
});
});
</script>
<div>
<h1>This is a non-jQuery div</h1>
</div>
<div id="jQuery_div">
<h2>This div should update with jQuery</h2>
</div>
<button id="updateBtn">update</button>
</body>
</html>
However, when flask delivers the web page on localhost:5000, the jQuery functionality is no longer present. My python is as follows:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def render():
return render_template("jquery_test.html")
if __name__ == "__main__":
app.run(port=5000, debug=True)
My app's file tree is:
/AJAX_practice
ajax_practice.py
/templates
jquery-3.3.1.js
jquery_test.html
I was trying to follow this tutorial when I couldn't get the "echo" button to work. In my efforts to debug, I have slowly chipped away and drastically simplified the program to the above code to see why I cannot get my jQuery to work through flask. I am still at a loss. I am running the flask app by pressing F5 in IDLE, with no errors in Python 2.7.13 Shell, and the Terminal (from which I started IDLE with $ sudo idle) showing:
my ip - - [date and time] "GET / HTTP/1.1" 200 -
my ip - - [date and time] "GET /jquery-3.3.1.js HTTP/1.1" 404 -
From this, my best guess is that flask cannot find the jquery.3.3.1.js file, though I have tried putting it everywhere in the file tree with no luck. I cannot use the script src to https for jQuery dependencies, as my server will eventually be on a non-internet connected LAN. Am I on the right track? If so, how does flask find and/or navigate jQuery dependencies? Can anyone point me towards some documentation that might help my fundamental understanding of this issue?
Any help on this matter would be greatly appreciated.
You are trying to serve JavaScript file from templates folder. Add a static folder and use that to serve static content.
in your case create a directory structure like "static/js/jquery.min.js"
and then add script reference like this
<script src="{{url_for('static', filename='js/jquery.min.js')}}"></script>
See this :
http://exploreflask.com/en/latest/static.html
If you don't want to keep it in "static" folder and use another local directory you can use send_from_directory as shown in this example :
https://stackoverflow.com/a/20648053/2118215
This has always worked for me with Flask in the past:
<script src="{{ url_for('static', filename='jquery-3.3.1.js') }}"></script>
'static' is the name of the folder it's in (and the 'static' folder is in the root of my project). You can edit this to suit your preferred structure and naming, so change 'static' to 'templates' if that's where you'd rather keep your jquery file, although I would recommend keeping it in a separate folder from your HTML templates, purely in the interests of keeping your project well organised.
I believe the path to jquery should be /templates/jquery-3.3.1.js
On me flask server when i serve jquery it has the full path from the home directory: /static/js/jquery.min.js
I am in the process of creating a Django web application that reads a URL and outputs selected data from the page. I have written the code in Python that parses the web page and it currently returns the information that I need to display in the Django app as desired.
Before I dive in I just want to confirm what I have researched is correct as I only have a limited time to complete the project.
To summarise my python code, it is in the src folder in a class called "manage.py"
I have created print statements that print the information that I need to display (I did this to ensure it was returning the correct data)
print(variable1)
print("some text" + variable2)
Can I create the Django app code in the same file, "manage.py"? (The project has already been created as a Django app in Eclipse when I started building the project)
Would I build the Django code as I've estimated below if I'm using the variables defined from the Python code above?
<!DOCTYPE>
<html>
<head>
<title>{% block title %}Title of website{% endblock %}</title>
</head>
<body>
<h1>Web page report</h1>
<h2>Summary of web page</h2>
<h3>Title of document</h3>
<p>{{variable1}}</p>
<h3>The file size of the document</h3>
<p>{"Some text" + {variable2}}</p>
</body>
</html>
Django has strict rules about where to place which information. You can not write everything into manage.py. Answering requests from the browser is for example done using view functions.
I'm prototyping an idea for a website that will use the HTML5 offline application cache for certain purposes. The website will be built with Python and Flask and that's where my main problem comes from: I'm working with those two for the first time, so I'm having a hard time getting the manifest file to work as expected.
The issue is that I'm getting 404's from the static files included in the manifest file. The manifest itself seems to be downloaded correctly, but the files that it points to are not. This is what is spit out in the console when loading the page:
Creating Application Cache with manifest http://127.0.0.1:5000/static/manifest.appcache offline-app:1
Application Cache Checking event offline-app:1
Application Cache Downloading event offline-app:1
Application Cache Progress event (0 of 2) http://127.0.0.1:5000/style.css offline-app:1
Application Cache Error event: Resource fetch failed (404) http://127.0.0.1:5000/style.css
The error is in the last line.
When the appcache fails even once, it stops the process completely and the offline cache doesn't work.
This is how my files are structured:
sandbox
offline-app
offline-app.py
static
manifest.appcache
script.js
style.css
templates
offline-app.html
This is the content of offline-app.py:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/offline-app')
def offline_app():
return render_template('offline-app.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
This is what I have in offline-app.html:
<!DOCTYPE html>
<html manifest="{{ url_for('static', filename='manifest.appcache') }}">
<head>
<title>Offline App Sandbox - main page</title>
</head>
<body>
<h1>Welcome to the main page for the Offline App Sandbox!</h1>
<p>Some placeholder text</p>
</body>
</html>
This is my manifest.appcache file:
CACHE MANIFEST
/style.css
/script.js
I've tried having the manifest file in all different ways I could think of:
CACHE MANIFEST
/static/style.css
/static/script.js
or
CACHE MANIFEST
/offline-app/static/style.css
/offline-app/static/script.js
None of these worked. The same error was returned every time.
I'm certain the issue here is how the server is serving up the files listed in the manifest. Those files are probably being looked up in the wrong place, I guess. I either should place them somewhere else or I need something different in the cache manifest, but I have no idea what. I couldn't find anything online about having HTML5 offline applications with Flask.
Is anyone able to help me out?
I would have thought this one would work:
CACHE MANIFEST
/static/style.css
/static/script.js
But in any case, you should not hardcode the URLs for your static files. It's best to serve the manifest as a template (moved to the "templates" folder) so that you can use url_for to generate the path to the static files, something like this:
CACHE MANIFEST
{{ url_for('static', filename='style.css') }}
{{ url_for('static', filename='script.js') }}
Then in your HTML template you would have a reference to a route instead of a static file:
<html manifest="{{ url_for('manifest') }}">
And finally, you would add a new view function that returns the manifest:
from flask import make_response
#app.route('/manifest')
def manifest():
res = make_response(render_template('manifest.appcache'), 200)
res.headers["Content-Type"] = "text/cache-manifest"
return res
I have written a little survey using Python and CGI. I am trying to show a picture using the normal <img> tag, But even-though the picture is in the same directory as my cgi script, my script cannot show it. I also changed the header to this:
print "Content-type: text/html; image/jpeg"
print
print """<html>
<head>
<title>You are going to be redirected</title>
</head>
<body bgcolor = #14b585>
<br>
<p align=center><font>Helloooo</font></p>
<img src="cat.jpeg" alt="cat" width="304" height="228"/>
<form action="./sample.py" method="post">
<p align=center><input type="submit" value="YES!" /></p>
</form>
</body>
</html>
"""
Why?(it is a very small jpg file)
print "Content-type: text/html; image/jpeg"
Don't change the header to that. You can't have multiple content types for a single document (multipart documents excluded, but browsers don't support them and that isn't the right format).
You are delivering an HTML document with an reference to an image in it. The image will be a separate request and response.
print "Content-type: text/html"
Or, better:
print "Content-type: text/html; charset=utf-8"
(Assuming you are using utf-8, which you should be).
print """<html>
Your Doctype is missing. This will trigger quirks mode, which is undesirable. You also have a great deal of legacy presentational markup that should be replaced by CSS.
<img src="cat.jpeg" alt="cat" width="304" height="228"/>
The context suggests that the image is decorative, so the alternative text should probably be "" (see Alt texts in IMGS), but there is nothing wrong with the actual reference to the image.
But even-though the picture is in the same directory as my cgi script
Since the HTML seems to be OK. You need to check that the image is.
Can you reach the image by typing the URL in directly?
What do the web server logs say when you try?
It is possible that your server is configured to try to treat everything in the directory as an executable, no matter what the file extension, so it might be trying to run the image as if it were a CGI program (which will obviously fail). If so, then you could either move the image or change the configuration of the server.
And I've just noticed this comment:
I did this in my browser: localhost/cgi-bin/cat.jpg and it got an error, I checked the logs, it Exec format error: exec of '/home/hossein/public_html/cgi-bin/cat.jpg' failed
That is what is happening. Moving the image is the simplest solution.
your apache was configured to use the cgi-bin directory as an CGI scripts folder, so any request that trying to get a file from this folder apache try to execute it as an CGI script. to make your image visible move it to the www/html folder.