Image won't load on python-based webserver - python

I've built a simple web server that gets a request and send a response. So when the server gets an invalid request, like "localhost/not-a-page", the server will send a response with the content of the HTML file "404.html" the webpage should display an image. So far, so good.
But when the 404 page loads up, the page can't find the image. The HTML part is correct and works offline. I've tried to move the image to serval locations, relative to the Python script, relative to the HTML. But it just can't find it. Hi I'm trying to make the server as low-level as I can, I want to learn how servers work. So I'm not using any server-related libraries. I'm using only the socket library of Python.
I'll appreciate any help to resolve this problem without using other libraries,
EDIT
Here is the relevant Python part :
import socket
import threading
import os
default_error_page = """\
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Error response</title>
</head>
<body>
<center>
<h1>Response</h1>
<p>Error code: %(code)d</p>
<p>Message: %(status)s.</p>
</center>
</body>
</html>
"""
default_header_status = "HTTP/1.1 %(code)d %(status)s\r\n"
default_header_content_type = "Content-Type: text/html; charset=utf-8\r\n\r\n"
buffer_size = 1024
def get_page(code):
    page = default_error_page
    if code == 200:
        pass
    else:
        file = open(os.path.dirname(__file__) + "/www/not-found.html", 'r')
        page = file.read()
    return page
class BaseServer:
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_name = ""
    host_name = ""
    host_port = 8000 # default port
    is_shutdown = False
    def __init__(self, name):
        self.server_name = name
    def start_server(self):
        thread = threading.Thread(target=self.run_server(), name=self.server_name)
        thread.start()
    def run_server(self):
        self.server_socket.bind((self.host_name, self.host_port)) # bind to host
        self.server_socket.listen()
        while not self.is_shutdown:
            conn, addr = self.server_socket.accept()
            self.handle_request(conn, addr)
    def handle_request(self, connection, address):
        print(str(address[0]) + " Connected! (port " + str(address[1]) + ")")
        result = self.parse_request(connection.recv(buffer_size))
        if result == 0:
            page = self.parse_response(200)
        else:
            page = self.parse_response(404)
        connection.sendall(bytes(page))
    def parse_request(self, data):
        if len(data) == 0:
            return
        strings = str(bytes(data).decode('utf-8')).split('\r\n')
        command, path, version = strings[0].split()
        print("command - " + command)
        print("path - " + path)
        print("version - " + version)
        status = 1
        if path == "/":
            status = 0
        return status
    def parse_response(self, code):
        status = "ERROR"
        if code == 200:
            status = "OK"
        elif code == 404:
            status = "NOT FOUND"
        base_header = (default_header_status % {'code': code, 'status': status})
        base_content_type = default_header_content_type
        # page = (default_error_page % {'code': code, 'status': status})
        page = str(get_page(code))
        return_string = str(base_header + base_content_type + page).encode('utf-8')
        print(return_string)
        return return_string
def main():
    server = BaseServer("Home Server")
    server.start_server()
if __name__ == "__main__":
    main()
And this is the HTML:
<html>
<head>
<link rel="stylesheet" type="text/css" href="/style/main.css"/>
<style>
*{
padding:0;
margin:0;
}
body{
background-color:#ffe6b3;
}
h1{
margin-top:30px;
background-color:#ffcc66;
font-size:3em;
display:inline-block;
color:#3a0000;
}
p{
margin-top:80px;
font-size:2em;
color:#3a0000;
}
#img404{
background-image:url(../images/404.gif);
width:100%;
height:50%;
background-repeat:no-repeat;
background-position:center 20%;
}
</style>
</head>
<body>
<center>
<div class=top>
<h1>ERROR 404</h1>
</div>
<p>
Sorry, we could not find the page :(
</p>
<div id="img404">
</div>
</center>
</body>
</html>
Sorry if it's not very readable, but I'm on the phone.
Dima.

Don't use relative paths for image like ../images/img.gif. Rather use full url or url relative to the root.
http://localhost/images/img.gif - full url
/images/img.gif - path relative to root url

Figured out my problem.
After I saw the logs, I realized that the browser sends another request, for the image.
And silly me, my code is:
if path ="/":
status = 0
else:
status = 1
So for every request which is not root("/") the server will return 404.
Oops

Related

How to show images from s3?

I'm finishing an application by django that generates pdf certificates
it was working great before I pull it into heroke and set up S3 amazom to host static files.
I have an html with the certificate template, and by HTML2PDF I render it to pdf. But it's not showing the background by css, it works just in the tag
Weird that if we opent the image url in s3 amazom it's shown perfectly
here the template css part
<meta charset="utf-8" />
{% load static %}
<style type="text/css">
#page {
size: 1122.52px 1587.4px ;
/*size: A4 landscape;*/
margin: 0cm;
background-image: url({{bg_front}});
height: 1588;
}
</style>
My view:
class ViewPDF(View):
def get(self, request, *args, **kwargs):
data = {}
pdf = True
if kwargs['pk']:
try:
participant = Participant.objects.get(pk=kwargs['pk'])
print(participant.cpf)
if participant.name:
certificate = Certificate.objects.get(pk=participant.certificate.pk)
pathBack = str(certificate.template.template_back.url)
pathFront = str(certificate.template.template_front.url)
print(pathFront)
#
# CONFIGURA OS BACKGROUNDS E TEXTO
#
data['bg_front'] = pathFront
data['bg_back'] = pathBack
setting = certificate.template.settings
start_date = datetime.strftime(certificate.start_date,'%d/%m/%Y')
end_date = datetime.strftime(certificate.start_date,'%d/%m/%Y')
data['text_front'] = setting.replace('<<nome>>',participant.name).replace('<<cpf>>',str(participant.cpf)).replace('<<ch>>',str(certificate.ch)).replace('<<instituicao>>',str(certificate.institution)).replace('<<DataInicio>>',start_date).replace('<<DataFim>>',end_date)
data['cpf'] = participant.cpf
pdf = render_to_pdf('app_certificates/body_front_pdf.html', data)
return HttpResponse(pdf, content_type='application/pdf')
except TypeError as e:
return HttpResponse(e)

Apache shows code not webpage on redirect

I have a webpage with a login form which is processed by a python script. When I enter a name that is not found in the DB, it redirects to a login_or_signup.html page. When I enter a name that is in the DB, is should redirect to a "next_page.html" page, but it shows some code. Why does it work one way and not the other? (I have changed it to go to the login_or_signup.html page both ways and it works both ways.
Python code
#! /usr/bin/python
import cgi
import cgitb
import mysql.connector as conn
import passlib
from passlib.hash import argon2
import logging
cgitb.enable(logdir="/var/www/html",
display=False,
format='text',
)
logging.basicConfig(level=logging.INFO)
def basic_top(data):
print """Content-type:text/html\n\n
<DOCTYPE html>
<html lang=en">
<head>
<meta charset= utf-8/>
<title>Basic Version</title>
</head>
<body>"""
for d in data:
print d
print
def go_back_top(data):
redirectURL = "%s" % "/login_or_signup.html"
try:
print """Content-type:text/html\n\n
Location: %s""" % redirectURL
print """<DOCTYPE html>
<html lang="en">
<head>
<meta charset= utf-8/>"""
print " <meta http-equiv='refresh' content='0;url=%s' />" % redirectURL
print """ <title>Login</title>
</head>\n\n
<body>"""
#for d in data:
# print d
# print
print ' Redirecting... Click here if you are not redirected' % redirectURL
except:
cgi.print_exception()
print "An error occured\n\n"
print"<PRE>"
cgitb.print_exc()
def go_forwards_top(data):
redirectURL = "%s" % "/login_or_signup.html" #/next_page.html"
try:
print """Content-type:text/html\n\n
Location: %s""" % redirectURL
print """<DOCTYPE html>
<html lang="en">
<head>
<meta charset= utf-8/>"""
print " <meta http-equiv='refresh' content='0;url=%s' />" % redirectURL
print """ <title>Login</title>
</head>\n\n
<body>"""
print ' Redirecting... Click here if you are not redirected' % redirectURL
except:
cgi.print_exception()
print "An error occured\n\n"
print"<PRE>"
cgitb.print_exc()
def htmlTail():
print "</body>"
print "</html>"
def getData():
formdata = cgi.FieldStorage()
username = formdata.getvalue("username")
password = formdata.getvalue("password")
login_type = formdata.getvalue("type")
if login_type == "signup":
password_copy = formdata.getvalue("password_copy")
return username, password, password_copy, login_type
else:
return username, password, login_type
def connectDB():
db = conn.connect(user='basic_user', password='twathead', host='127.0.0.1', db="exampleDB")
cursor = db.cursor()
logging.info(cursor)
return db, cursor
def check_person(cursor, data):
username = data[0]
sql_str = "SELECT * FROM people WHERE username = '%s'" % (username)
cursor.execute(sql_str)
person = cursor.fetchone()
if person is None:
go_back_top(data)
else:
go_forwards_top(data)
#logging.info(str(conn))
#conn.close()
if __name__=="__main__":
logging.basicConfig(filename='/usr/lib/cgi-bin/example.log', level=logging.INFO)
logging.info('This message should go to the log file')
#data = ("John", "Smith")
data = getData()
#basic_top(data)
#print "login type = "
#print data[0]
db, cursor = connectDB()
check_person(cursor, data)
Html for next_page:
<DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="mystyle.css">
<title>Post Login</title>
</head>
<body style="background-color:powderblue;">
<h2>This page is currently under construction.<h2>
</body>
</html>
Open terminal, type:
service apache-htcacheclean restart

Python flask stream not updating

I'm trying to stream some data, to update progress bar on my website. For testing I'm running this:
#application.route('/api/stream', methods=['GET', 'POST'])
def stream():
def event_stream():
n = 0
while True:
yield "data: %s\n\n" % n
n += 1
time.sleep(1)
return Response(event_stream(), mimetype='text/event-stream')
#application.route('/api/output')
def index():
return """
<!DOCTYPE html>
<html>
<head>
<script>
if(typeof(EventSource)!=="undefined") {
var source=new EventSource("/api/stream");
source.onmessage=function(event) {
document.getElementById("result").innerHTML+=event.data + "<br>";
};
source.update=function(event) {
document.getElementById("result").innerHTML+=event.data + "<br>";
};
} else {
document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";
}
</script>
</head>
<body>
<h1>Data</h1>
<div id="result"></div>
</body>
</html>
"""
and on my website I've got post function:
$.post("//mywebsite.com/api/stream", {"some": 'ss'}, function(data) {
alert(data)
})
And when I run this POST (above) then I'm opening mywebsite.com/api/output and it displays only:
Data
But when after a while I stop my flask app, on the website I'm getting:
Data
0
1
2
Why It's not updating live but only when I stop flask app?

Line numbers in Pygments code highlight in xampp on Windows

I have configured xampp on windows to work with python 2.7 and Pygments. My php code is highlighted properly in Pygments on the website. The code has colors, span elements, classes.
That is how it looks:
But I cannot get line numbers.
As I have read tutorials it depends on the linenos value in python script. The value should be either table or inline or 1 or True.
But it does not work for me. I still gives the same final code
<!doctype html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="gh.css">
</head>
<body>
<div class="highlight highlight-php"><pre><code><span class="nv">$name</span> <span class="o">=</span> <span class="s2">"Jaś"</span><span class="p">;</span>
<span class="k">echo</span> <span class="s2">"Zażółć gęślą jaźń, "</span> <span class="o">.</span> <span class="nv">$name</span> <span class="o">.</span> <span class="s1">'.'</span><span class="p">;</span>
<span class="k">echo</span> <span class="s2">"hehehe#jo.io"</span><span class="p">;</span>
</code></pre></div>
</html>
How to add line numbers? I put two files of the website below:
index.py
import sys
from pygments import highlight
from pygments.formatters import HtmlFormatter
# If there isn't only 2 args something weird is going on
expecting = 2;
if ( len(sys.argv) != expecting + 1 ):
exit(128)
# Get the code
language = (sys.argv[1]).lower()
filename = sys.argv[2]
f = open(filename, 'rb')
code = f.read()
f.close()
# PHP
if language == 'php':
from pygments.lexers import PhpLexer
lexer = PhpLexer(startinline=True)
# GUESS
elif language == 'guess':
from pygments.lexers import guess_lexer
lexer = guess_lexer( code )
# GET BY NAME
else:
from pygments.lexers import get_lexer_by_name
lexer = get_lexer_by_name( language )
# OUTPUT
formatter = HtmlFormatter(linenos='table', encoding='utf-8', nowrap=True)
highlighted = highlight(code, lexer, formatter)
print highlighted
index.php
<?php
define('MB_WPP_BASE', dirname(__FILE__));
function mb_pygments_convert_code($matches)
{
$pygments_build = MB_WPP_BASE . '/index.py';
$source_code = isset($matches[3]) ? $matches[3] : '';
$class_name = isset($matches[2]) ? $matches[2] : '';
// Creates a temporary filename
$temp_file = tempnam(sys_get_temp_dir(), 'MB_Pygments_');
// Populate temporary file
$filehandle = fopen($temp_file, "w");
fwrite($filehandle, html_entity_decode($source_code, ENT_COMPAT, 'UTF-8'));
fclose($filehandle);
// Creates pygments command
$language = $class_name ? $class_name : 'guess';
$command = sprintf('C:\Python27/python %s %s %s', $pygments_build, $language, $temp_file);
// Executes the command
$retVal = -1;
exec($command, $output, $retVal);
unlink($temp_file);
// Returns Source Code
$format = '<div class="highlight highlight-%s"><pre><code>%s</code></pre></div>';
if ($retVal == 0)
$source_code = implode("\n", $output);
$highlighted_code = sprintf($format, $language, $source_code);
return $highlighted_code;
}
// This prevent throwing error
libxml_use_internal_errors(true);
// Get all pre from post content
$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding('
<pre class="php">
<code>
$name = "Jaś";
echo "Zażółć gęślą jaźń, " . $name . \'.\';
echo "<address>hehehe#jo.io</address>";
</code>
</pre>', 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NODEFDTD);
$pres = $dom->getElementsByTagName('pre');
foreach ($pres as $pre) {
$class = $pre->attributes->getNamedItem('class')->nodeValue;
$code = $pre->nodeValue;
$args = array(
2 => $class, // Element at position [2] is the class
3 => $code // And element at position [2] is the code
);
// convert the code
$new_code = mb_pygments_convert_code($args);
// Replace the actual pre with the new one.
$new_pre = $dom->createDocumentFragment();
$new_pre->appendXML($new_code);
$pre->parentNode->replaceChild($new_pre, $pre);
}
// Save the HTML of the new code.
$newHtml = "";
foreach ($dom->getElementsByTagName('body')->item(0)->childNodes as $child) {
$newHtml .= $dom->saveHTML($child);
}
?>
<!doctype html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="gh.css">
</head>
<body>
<?= $newHtml ?>
</body>
</html>
Thank you
While reading the file try readlines:
f = open(filename, 'rb')
code = f.readlines()
f.close()
This way you do the following it will get multiple lines :
formatter = HtmlFormatter(linenos='table', encoding='utf-8', nowrap=True)
Suggestion:
More pythonic way of opening files is :
with open(filename, 'rb') as f:
code = f.readlines()
That's it python context manager closes this file for you.
Solved!
nowrap
If set to True, don’t wrap the tokens at all, not even inside a tag. This disables most other options (default: False).
http://pygments.org/docs/formatters/#HtmlFormatter

Showing web camera through web interface (Python, CherryPy, OpenCV)

I want to show OpenCV processed image with web interface (made with CherryPy). Code below works fine:
import cherrypy
import cv2
class Picture(object):
def __init__(self):
self.cam = cv2.VideoCapture(0)
#cherrypy.expose
def index(self):
_, image = self.cam.read()
_, data = cv2.imencode('.jpg', image)
cherrypy.response.headers['Content-Type'] = 'image/jpeg'
return data.tostring()
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '127.0.0.1', 'server.socket_port': 80, })
cherrypy.quickstart(Picture())
However: I would like to embed the image in html so I can (for example) add another image or other data to the same page.
I have tried the following code:
import cherrypy
import cv2
class Picture(object):
def __init__(self):
self.cam = cv2.VideoCapture(0)
#cherrypy.expose
def index(self):
_, image = self.cam.read()
_, data = cv2.imencode('.jpeg', image)
return """ <html>
<head>
<title>CherryPy static imagl</title>
</head>
<html>
<body>
<img src=" """ + data + """:image/jpeg" />
</body>
</html>"""
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '127.0.0.1', 'server.socket_port': 80, })
cherrypy.quickstart(Picture())
But this gives the following error:
<img src=" """ + data + """:image/jpeg" />
TypeError: cannot concatenate 'str' and 'numpy.ndarray' objects
Converting the numpy arry to a string using the following code also does not work (it gives no error but displays only characters):
<img src=" """ + data.tostring() + """:image/jpeg" />
Anyone who can give me some more insight? Thanks in advance!
The following code does the trick :)
import cherrypy
import cv2
import base64
class Picture(object):
def __init__(self):
self.cam = cv2.VideoCapture(0)
#cherrypy.expose
def index(self):
_, image = self.cam.read()
_, data = cv2.imencode('.jpg', image)
jpeg_base64 = base64.b64encode(data.tostring())
return """
<html>
<head>
<meta http-equiv="refresh" content="1" />
<title>Cherrypy webcam</title>
</head>
<html>
<body>
<img src='data:image/jpeg;base64,%s' />
<img src='data:image/jpeg;base64,%s' />
</body>
</html>
""" % (jpeg_base64, jpeg_base64)
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '127.0.0.1', 'server.socket_port': 80, })
cherrypy.quickstart(Picture())
This code displays the same picture two times. and the:
<meta http-equiv="refresh" content="1" />
refreshes the code every second.

Categories

Resources