Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:25:11+00:00 2026-05-27T14:25:11+00:00

In Tornado, how do you tell apart the different request types? Also, what is

  • 0

In Tornado, how do you tell apart the different request types? Also, what is the proper way to split out the requests? In the end if I go to /item/1.xml, I want xml, /item/1.html to be a proper html view etc.

Something like:

def getXML():
    return self.render('somexmlresult.xml')

def getHTML():
    return self.rendeR('htmlresult.html')

or

def get():
    if request == 'xml':
        return self.render('somexmlresult.xml')
    elif request == 'html':
        return self.render('htmlresult.html')

~ edit ~ I was shooting for something along the lines of rails’ implementation seen here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:25:11+00:00Added an answer on May 27, 2026 at 2:25 pm

    First, set up the handlers to count on a restful style URI. We use 2 chunks of regex looking for an ID and a potential request format (ie html, xml, json etc)

    class TaskServer(tornado.web.Application):
        def __init__(self, newHandlers = [], debug = None):
            request_format = "(\.[a-zA-Z]+$)?"
            baseHandlers = [
                (r"/jobs" + request_format, JobsHandler),
                (r"/jobs/", JobsHandler),
                (r"/jobs/new" + request_format, NewJobsHandler),
                (r"/jobs/([0-9]+)/edit" + request_format, EditJobsHandler)
            ]
            for handler in newHandlers:
                baseHandlers.append(handler)
    
    
        tornado.web.Application.__init__(self, baseHandlers, debug = debug)
    

    Now, in the handler define a reusable function parseRestArgs (I put mine in a BaseHandler but pasted it here for ease of understanding/to save space) that splits out ID’s and request formats. Since you should be expecting id’s in a particular order, I stick them in a list.

    The get function can be abstracted more but it shows the basic idea of splitting out your logic into different request formats…

    class JobsHandler(BaseHandler):
        def parseRestArgs(self, args):
            idList = []
            extension = None
            if len(args) and not args[0] is None:
                for arg in range(len(args)):
                    match = re.match("[0-9]+", args[arg])
                    if match:
                        slave_id = int(match.groups()[0])
    
                match = re.match("(\.[a-zA-Z]+$)", args[-1])
                if match:
                    extension = match.groups()[0][1:]
    
            return idList, extension
    
        def get(self, *args):
            ### Read
            job_id, extension = self.parseRestArgs(args)
    
            if len(job_id):
                if extension == None or "html":
                   #self.render(html) # Show with some ID voodoo
                   pass
                elif extension == 'json':
                    #self.render(json) # Show with some ID voodoo
                    pass
                else:
                    raise tornado.web.HTTPError(404) #We don't do that sort of thing here...
            else:
                if extension == None or "html":
                    pass
                    # self.render(html) # Index- No ID given, show an index
                elif extension == "json":
                    pass
                    # self.render(json) # Index- No ID given, show an index
                else:
                    raise tornado.web.HTTPError(404) #We don't do that sort of thing here...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a tornado request handler if I have to call function foo() which doesn't
I'm starting to develop a simple Tornado application, and I'd like to see request
Tornado is a webserver + framework like Django but for real-time features. On my
I've been working on an embedded C/C++ project recently using the shell in Tornado
I'm trying to adapt some tornado code to work with twisted. Tornado's IOLoop has
Tornado web framework seems to expose a method RequestHandler.prepare() which is called before the
I'm using the Tornado framework (Python). I have the sluggable URLs working. But I
I have a Python Tornado server sitting behind a nginx frontend. Every now and
In the tornado .web module there is a function called _time_independent_equals : def _time_independent_equals(a,
I have a simple tornado server running like this: import json import suds from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.