Live data from Hacker News

I only know PHP. How do I write a Web application in Python?

me.veekun.com

121–130 of 196 posts

Re: I only know PHP. How do I write a Web application in Python?

#121
post #52

Earlier quoted context omitted.

CGI scripts have much of PHPs simplicity. You can just drop in a executable file, and depending bit on server configuration, it will just run. With Python a basic hello world would look something like this: #!/usr/bin/python print "Content-type: text/plain" print "" print "Hello World!" So 3 lines of "boilerplate", and then you can do anything you want. No extra packages to install or languages to learn. No framework…

And therein lies the problem, in WSGI 99% of web sites are hosted in "shared hosting" services Now compare copying the .php files with configuring WSGI "No, but WSGI is easy blah blah blah" it doesn't beat copying files And no, settings WSGI is not easy Also, if you depend on IIS for example, PHP is almost plug'n'play WSGI on Windows is the definition of "bag of hurt". You can try fastcgi to run on windows, and then…

FastCGI works fairly well in my experience on shared Linux servers (that's how I host my personal projects). And for smaller scripts where I don't bother setting up FCGI, I just use plain CGI, which is just copying files.

Re: I only know PHP. How do I write a Web application in Python?

#122

Is there any cheap & easy webhosting available for Python? Occasionally I get commisioned to build small websites ( I don't want the hassle/expense of having to setup and run VPS/AWS infastructure. I just want to give someone a bunch of files and say "sign up for some cheap webhosting, ask them to give you a DB called X and upload these". I usually end up just doing it in PHP, but it has always felt like a poor reaso…

I've had a lot of great luck with dotcloud.

http://docs.dotcloud.com/services/python/

Re: I only know PHP. How do I write a Web application in Python?

#123
post #88
post #23

Earlier quoted context omitted.

Fair point. Correction: working with encodings sucks. Py3 isn't magnitudes better with Unicode; it's better about converting incoming bytes, and it makes literals into real strings. But the former isn't necessarily a good thing, and you can get the latter with a `__future__` import. Always dying on implicit conversions is definitely nice, though.

Working with encodings is indeed annoying in Python, but it’s not a “universal” problem.

There is a language called 'Java' where every string is in unicode, and I do not remember ever running into an issue!

Re: I only know PHP. How do I write a Web application in Python?

#124

The idea is that a transaction starts when a request starts, and it’s automatically rolled back if there’s an exception. This is behavior you want from the start! It’s half (err, ¼) the point of using a database. I mostly agreed with the rest of the article but this strikes me as massive blooper. I can see no reason this would be considered good advise. It's the database equivalent of the GIL, can't figure out what y…

> An example of this going wrong in production, I work in the online gaming (gambling) industry. One of our users won a large jackpot, our code started a transaction, contacted the 3rd party to have them release the funds, recorded the win in the transaction journal, updated the customers balance etc. Then the stored procedure which inserted a row into the outgoing email queue had a small error. This caused all of the database work on our end to rollback.

Updating a customer's winnings and recording a win are definitely things that should be together in a transaction - if one happened and not the other, that's a failure.

This is not the fault of using a transaction, this is the fault of not working with what is essentially a distributed transaction. Since you are relying on a message against a third party, you need to build distribution into this system, which is challenging. You might contact the third party after you've completed the internal processing, and you might use two-phase semantics even such that you can "PREPARE TRANSACTION" the work you've done locally, (nearly) guaranteeing that a commit will proceed, then work with your external messaging, then commit locally.

background at http://en.wikipedia.org/wiki/Two-phase_commit_protocol

Re: I only know PHP. How do I write a Web application in Python?

#125
post #18

Earlier quoted context omitted.

What frameworks and platforms would you recommend for web development with java? I am writing my senior project at college in java (mainly because of the wide amount of image processing libraries available) and would love to add web capabilities.

I tend to prefer Python/Flask for Web dev to Python but if you are inclined to use Java there's only really one framework that doesn't make me want to tear my eyeballs out. That framework is Spring MVC. It is DI, adapters for vendor-specific libraries and utility. It's probably not the easiest thing to start with just because it is so flexible that it can be hard to know where to start. A good "Hello World" Spring MV…

It's probably not the easiest thing to start with just because it is so flexible that it can be hard to know where to start. A good "Hello World" Spring MVC 3.1 app would go a long way.

Try http://blog.springsource.com/2011/01/04/green-beans-getting-...

Re: I only know PHP. How do I write a Web application in Python?

#126
post #52

Earlier quoted context omitted.

CGI scripts have much of PHPs simplicity. You can just drop in a executable file, and depending bit on server configuration, it will just run. With Python a basic hello world would look something like this: #!/usr/bin/python print "Content-type: text/plain" print "" print "Hello World!" So 3 lines of "boilerplate", and then you can do anything you want. No extra packages to install or languages to learn. No framework…

Let's look at the PHP version: Hello World! One line. I didn't have to worry about Linux/Windows, knowing HTTP headers or the fact that I need two CRLFs after the last header[1]. I just wanted a page that says "Hello World!" and that's what I got. Don't get me wrong, I would never choose to write a web app in PHP over python. But it is brain dead simple to go from nothing to something in a few keystrokes. Which is pr…

In the context of the parent comment I'd argue that the differences are insignificant. Xuzz argued that PHP is simpler because you don't need to know the ecosystem. Which I countered by showing a basic example of Python which doesn't depend on the ecosystem (like frameworks, package managers or version control).

Besides that, I don't think knowing HTTP headers or understanding significance of trailing newlines is required to write CGI scripts. You only need to know that you need those three 'magic' lines in the front of your scripts. Similarly like you need to know that in php you need around your script.

Re: I only know PHP. How do I write a Web application in Python?

#127

Personally, I _like_ PHP because, at the core, it is a collection of utility functions to write webapps quickly. Here's a small set of those function that I would like to see somewhere else (like in Python for example): Save a file: file_put_contents() Load a file: file_get_contents() Trim whitespace: trim() Print a string (even numbers!): print $str Dump an array: print_r() Print the stack trace: print $e->getTraceA…

In Ruby:

    # Save a file
    f.write(str)

    # Read a file
    f.read

    # Trim whitespace
    " foo ".strip

    # Print anything
    puts 1 # even numbers!
    puts File.open("foo", "w+") # even file handles!
    puts [1, 2, 3, 4] # even arrays!
    puts [].class # even classes!
    puts [].method(:size) # even functions!

    # Dump an array (see above)
Python is similarly clear:

    # Save a file
    f.write(str)

    # Read a file
    f.read()

    # Trim whitespace
    " foo ".strip()

    # Print anything
    print 1 # even numbers!
    print open("foo", "w+") # even file handles!
    print [1, 2, 3, 4] # even arrays!
    print [].__class__ # even classes!
    puts [].remove # even functions!

    # Dump an array (see above)
Ruby and Python are extremely well documented. They have great standard libraries which wrap existing C functions too...and wrap them in a consistent fashion. They are also the second and third most popular language on Github, respectively...which means tons of great libraries to solve almost any problem you can think of.

Ruby and Python are just as easy to use as PHP.

Re: I only know PHP. How do I write a Web application in Python?

#128

Earlier quoted context omitted.

The transaction is internal to the database. It's essentially a list of operations to perform, if something goes wrong it provides a list of things to reverse. If the db transaction fails, then there would need to be some code to handle rolling back operations outside of the database. I don't think db transactions were to fault in your situation.

I'm not sure I really get what you are trying to say. I know what a transaction is, and I know it wasn't the fault of transactions. It was the fault of our poor use of transactions, in the same monolithic transaction pattern that the OP appears to be advocating.

transaction-per-request is by far the best default pattern to use. If some particular methods have special needs, like needing to break up the operations into multiple transactions to deal with third-party communication, those are the exceptions.

The alternatives to transaction-per-request are autocommit, or explicit transactions required at all times. Autocommit is a terrible choice because now you've lost all atomicity and isolation for dependent operations (locking is also not much of an issue. If you're doing VB, then you're on SQL server, which has some of the worst locking behavior - turn on snapshot isolation to make it bearable). Explicit transactions required at all times is a terrible choice because now your app is littered with what is 90% of the time unnecessary boilerplate.

Re: I only know PHP. How do I write a Web application in Python?

#129

I like java for web app development, simply because it has a well-structured api and a massive community of developers who have run in to the majority of the road blocks you will most likely encounter on the way to launching your app.

What frameworks and platforms would you recommend for web development with java? I am writing my senior project at college in java (mainly because of the wide amount of image processing libraries available) and would love to add web capabilities.

I'm writing an AJAX webapp using Dropwizard, and seems that I won't be hitting any roadblocks anymore. The devs are very helpful on the google groups as well.

Re: I only know PHP. How do I write a Web application in Python?

#130
post #121

Earlier quoted context omitted.

And therein lies the problem, in WSGI 99% of web sites are hosted in "shared hosting" services Now compare copying the .php files with configuring WSGI "No, but WSGI is easy blah blah blah" it doesn't beat copying files And no, settings WSGI is not easy Also, if you depend on IIS for example, PHP is almost plug'n'play WSGI on Windows is the definition of "bag of hurt". You can try fastcgi to run on windows, and then…

FastCGI works fairly well in my experience on shared Linux servers (that's how I host my personal projects). And for smaller scripts where I don't bother setting up FCGI, I just use plain CGI, which is just copying files .

might want to not do that... http://www.php.net/archive/2012.php#id2012-05-03-1
Post reply on HN