1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How load SVG from BaseHTTPServer in python?

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    I created a server with python.

    Now I have a webpagecgi to screen my result and also a svg file. I tested 3 ways to that: Solution1 a javascript method to load quicky the svg (works out of server-> but not in the localserver) Solution2 a simple object import (works out of server -> but not in the localserver) Solution3 svg inside the html (works out and in the server)

    I would like to use the solution 1 or 2 because the 3 is not comfortable and dirty for me... Why Solution1 and 2 does not work? I think they do not find svg file but I don't understand why...

    os.system("cp %s WEBSERVER/toto.svg"%(svg))
    print 'Content-type: text/html' #application/xhtml+xml'# image/svg+xml'
    print '''
    <html>
    <head>
    <script language="javascript">
    function loadSVG(id, svg) {
    var element = document.getElementById(id);
    var object = document.createElement("object");
    object.type = "image/svg+xml";
    object.data = svg;
    element.appendChild(object);
    }
    </script>
    </head>

    <body>


    SOLUTION1<br/>
    <script type="text/javascript">
    loadSVG('hca', 'WEBSERVER/toto.svg');
    </script>

    SOLUTION2<br/>
    <object type='image/svg+xml' data ='WEBSERVER/toto.svg'></object>

    SOLUTIONT3<br/>
    <svg xmlns="http://www.w3.org/2000/svg"
    width="800" height="800" >
    <rect x="50" y="200" width="250" height="40" />
    </svg>
    </body>

    </html>
    '''


    Here is the webserver that I launch in python:

    #!/usr/bin/python

    import BaseHTTPServer
    import CGIHTTPServer
    import cgitb; cgitb.enable()

    server = BaseHTTPServer.HTTPServer
    handler = CGIHTTPServer.CGIHTTPRequestHandler
    server_address = ("0.0.0.0", 8123)
    handler.cgi_directories = ['/WEBSERVER']

    httpd = server(server_address, handler)

    try:
    print "Running HTTP server"
    httpd.serve_forever()
    except KeyboardInterrupt:
    print "Server Stoped"

    Continue reading...

Compartilhe esta Página