4

I'm worked on PHP previously and want to know python web programming. But everything for me is ambiguous.

Is it possible to program web app using python without "any" framework?

I know frameworks make everything easy for us and even if it is possible program web app without framework , it is not logical for big projects. But my target is just learning because I think start programming with frameworks is not good idea and we should know some information about pure python in web applications. So Imagine I want make very very simple webpage like hello world or small counter or small api. please do not offer me lightweight frameworks like flask. I just want know can pure python create webpages?

5
  • 2
    Yes, that's possible. Commented Mar 8, 2019 at 22:35
  • 1
    it's really just a matter of how much re-inventing the wheel you want to do... Python can listen and respond on tcp ports using the socket library, or you could go one step further and use the http.server library which breaks out a bit more functionality for you, but you still are just working with the exact get, post, delete, etc. requests. Commented Mar 8, 2019 at 22:35
  • This link will answer your question Python web development with or without frameworks Commented Mar 8, 2019 at 22:38
  • 1
    If learning is the goal, here is a good 3-hour PyCon workshop on that topic by one of the co-creators of Django, which goes into exactly what makes up a web framework - "Let's Build A Web Framework" Commented Mar 8, 2019 at 22:46
  • Yes, you can use good old CGI with the builtin cgi module like we used to do 20 years ago (TBH I used Perl CGI back then but same principles apply). Commented Mar 8, 2019 at 23:15

1 Answer 1

2

Yes, technically, you can create a webpage with "pure python," but you certainly wouldn't want to! The most beautiful part of programming is the fact that there are millions of others who have worked hard to create "frameworks," such as Flask or Django, that massively simplify the work that is required to make an application.

If your goal is "just learning" then using a framework that you are unfamiliar with is a great way to start. Learning programming is not about the technical knowledge required to run every aspect of your code, rather it is all about knowing how to ask the right questions for the particular application you are trying to build.

Programmers are lazy. If there is a shortcut that can be taken in code, you should take it (unless you have a really good reason not to).

Think of this example:

I have a list that was converted to a string:

myStringList = "['foo', 'bar', 'foobar']"

How can I convert this string to a format that my computer can interpret as a list?

I could make a function like this:

def stringToList(node_or_string):
    if isinstance(node_or_string, str):
        node_or_string = parse(node_or_string, mode='eval')
    if isinstance(node_or_string, Expression):
        node_or_string = node_or_string.body
    def _convert_num(node):
        if isinstance(node, Constant):
            if isinstance(node.value, (int, float, complex)):
                return node.value
        elif isinstance(node, Num):
            return node.n
        raise ValueError('malformed node or string: ' + repr(node))
    def _convert_signed_num(node):
        if isinstance(node, UnaryOp) and isinstance(node.op, (UAdd, USub)):
            operand = _convert_num(node.operand)
            if isinstance(node.op, UAdd):
                return + operand
            else:
                return - operand
        return _convert_num(node)
    def _convert(node):
        if isinstance(node, Constant):
            return node.value
        elif isinstance(node, (Str, Bytes)):
            return node.s
        elif isinstance(node, Num):
            return node.n
        elif isinstance(node, Tuple):
            return tuple(map(_convert, node.elts))
        elif isinstance(node, List):
            return list(map(_convert, node.elts))
        elif isinstance(node, Set):
            return set(map(_convert, node.elts))
        elif isinstance(node, Dict):
            return dict(zip(map(_convert, node.keys),
                            map(_convert, node.values)))
        elif isinstance(node, NameConstant):
            return node.value
        elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):
            left = _convert_signed_num(node.left)
            right = _convert_num(node.right)
            if isinstance(left, (int, float)) and isinstance(right, complex):
                if isinstance(node.op, Add):
                    return left + right
                else:
                    return left - right
        return _convert_signed_num(node)
    return _convert(node_or_string)

And call it like this:

myList = stringToList(myStringList)

print(myStringList[0] # [
print(myList[0]) # foo

Or I could simply use the wonderful ast package in the default python library and achieve the same results:

import ast

myList = ast.literal_eval(myStringList)

print(myList[0]) # foo

All credit for the stringToList function goes to the creators of the ast package, as I merely copied it from source code.

To add an example of a "good reason" to create your own package/framework, lets say I need to use the eval function for whatever reason. As we all know, eval is dangerous and, generally speaking, shouldn't be used. If I really need to use it, and there aren't any alternatives (cough cough, literal_eval), the only solution would be to create my own version of the eval function, to avoid having exploitable code.

Sign up to request clarification or add additional context in comments.

4 Comments

better yet, you could not convert your objects to str and call that serialization! +1
Did you mean programm without framework is so hard that we couldn't make even hello world page without framework easily??? Can a intermediate python programmer build a very simple page but he doesn't? or it is advanced subject?
@AliMarasizadeh sorry, I don't understand your question.
sorry for my bad english :'( I want to know create hello world page with pure python on web is hard? is it professional case? I hope that you understood my question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.