Forums

how to make wsgi config with 2 class app

this my file tuto08.py

import os, os.path
import random
import string

import cherrypy


class StringGenerator(object):
    @cherrypy.expose
    def index(self):
        return open('index.html')


@cherrypy.expose
class StringGeneratorWebService(object):

    @cherrypy.tools.accept(media='text/plain')
    #@cherrypy.expose
    def GET(self):
        return cherrypy.session['mystring']

   #@cherrypy.expose
    def POST(self, length=8):
        some_string = ''.join(random.sample(string.hexdigits, int(length)))
        cherrypy.session['mystring'] = some_string
        return some_string

   #@cherrypy.expose
    def PUT(self, another_string):
        cherrypy.session['mystring'] = another_string

   #@cherrypy.expose
    def DELETE(self):
        cherrypy.session.pop('mystring', None)


if __name__ == '__main__':
    conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/generator': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.response_headers.on': True,
            'tools.response_headers.headers': [('Content-Type', 'text/plain')]
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': './public'

        },

        '/js': {
            'tools.staticdir.on':True,
            'tools.staticdir.dir': "./public/js"
            },
        '/css':{
            'tools.staticdir.on':True,
            'tools.staticdir.dir': "./public/css"
            }
    }
    webapp = StringGenerator()
    webapp.generator = StringGeneratorWebService()
    cherrypy.quickstart(webapp, '/', conf)

this my config wsgi got error 404nf POST

import sys
sys.stdout = sys.stderr

import cherrypy

code_directory = "/home/Wicaksono/Kuliah04/"
if code_directory not in sys.path:
    sys.path.insert(0, code_directory)
from tuto08 import StringGenerator
application = cherrypy.Application(StringGenerator(),script_name='', config=None)

[edited by admin: formatting]

You could perhaps ask this on StackOverflow with a CherryPy tag?

i am new here to learn python , its make me confuse. because i got problem with POST generate

jquery-3.3.1.min.js:2 POST http://wicaksono.pythonanywhere.com/generator 404 (Not Found)
send @ jquery-3.3.1.min.js:2
ajax @ jquery-3.3.1.min.js:2
w.(anonymous function) @ jquery-3.3.1.min.js:2
(anonymous) @ (index):12
dispatch @ jquery-3.3.1.min.js:2
y.handle @ jquery-3.3.1.min.js:2

my program still work at localhost

[edit by admin: formatting]

If you're getting a 404, it means that you haven't defined a handler for the /generator/ URL. That's because you're defining all of your URLs in the if __name__ == "__main__" section, and that's not run on PythonAnywhere.

We can't give detailed instructions on how to code CherryPy here, but what you need to ask on Stack Overflow is how to configure the WSGI file for a website with multiple classes. WSGI is a general system for interfacing your CherryPy app with a web server -- it's not a PythonAnywhere-specific thing -- so someone should be able to help you.