~abyxcos/python_tutorial

13703e3d2d4dd226388f22f439dfe14b76311bce — abyxcos 4 years ago 8732dba master
Specify our code blocks as type:python
1 files changed, 5 insertions(+), 5 deletions(-)

M README.md
M README.md => README.md +5 -5
@@ 12,7 12,7 @@ As a first step in my projects, I like to get them to a point where I can see th

Let's create a new python in our favorite editor and name it `journal.py`. The following code is the bare minimum needed to talk to the internet, but be warned: that is all it does. If you run it now, you'll see what I mean. The program "runs fine", but if you connect your browser to [http://localhost:8000](http://localhost:8000) you'll see that while it can talk to the internet just fine... it has nothing to say to your browser. Just a nice error telling you that it has nothing to say to your browser. Let's take a look at the code we have first, and then add the parts we need so we can talk to any browser, not just the internet. 

```
```python
import http.server

httpd = http.server.HTTPServer(


@@ 32,7 32,7 @@ The final line, the `serve_forever` does exactly what it suggests. It sits there

Since we have some working code in our program now, it's a good time to start backing up our code so we can trace our steps backwards if something breaks. We'll use `git` for this, as it's fast becoming the industry standard for document and change control (also referred to as revision control, source code control, or history management). If you are using a fancier editor or a full development environment (integrated development environment or IDE), you may have a button or menu to handle this for you. However, the following raw git commands will create a new git repository in your folder, tell git about the file you expect to be changing, and then add a checkpoint you can look up later. 

```
```python
git init
git add journal.py
git commit -m "This is a basic web request handler, but nothing else yet."


@@ 40,7 40,7 @@ git commit -m "This is a basic web request handler, but nothing else yet."

If you have an account (or wish to create an account) on a code sharing site, such as [GitLab](https://gitlab.com/) or [GitHub](https://github.com/) you may want to choose now to also upload your project there to show off your progress or to be able to work continue work from multiple computers. Click the `New Project` button on the site and name your project something descriptive like `Web_Journal` or `Python_Journal`. I named my project `python_tutorial`, choosing underbars (`_`) instead of spaces as spaces usually cause problems with computers. The following two git commands link up your code sharing account with your project and then upload (push) your files to the website. 

```
```python
git remote add origin git@gitlab.com:abyxcos/python_tutorial.git
git push -u origin master
```


@@ 51,7 51,7 @@ If git has already been set up on your computer, this should just work. If you h

Now that we have our program talking to the internet, let's take the next step and set up the full pipeline so that rather than stopping when your computer makes an internet connection, the program will proceed to continue the conversation and send a message to the browser. This is accomplished simply by popping out that placeholder `BaseHttpRequestHandler` and crafting our own class to handle requests how we want them. If you haven't bumped into classes yet, classes are just a way of grouping a bunch of generic functions into a package that anyone can easily extend. Classes differ from libraries in that while both group up a set of functions, libraries give you code in a take it or leave it method. Classes however give you a template of functions that you can pick, replace, or extend  as you need. In Python a lot of core libraries (including `http.server`) actually provide classes rather than raw functions allowing us this freedom. `BaseHTTPRequestHandler` actually provides a lot of code we want to take advantage of already, but it's missing one particular function that were are very interested in; the `do_GET` function that gets called any time a browser opens a connection to our server and wants to get a page. Right now the browser is just stopping at opening the connection because we don't have that function to give it a page created yet. So let's make a `JournalRequstHandler` class to handle people requesting pages off our journal, link it up to the `BaseHttpRequestHandler`, and then make a simple `do_GET` function to do something useful when someone tries to get our page. 

```
```python
import http.server

class JournalRequestHandler(http.server.BaseHTTPRequestHandler):


@@ 74,7 74,7 @@ As you can see, our core program is still the same, we've just added our new req

As we now have another working feature, I make it a habit to always create a checkpoint in git, no matter how simple the feature. Using `git commit` with the `-a` flag tells git to automatically add any changed files it already knows about (is tracking) to the commit group for the checkpoint. If you want to manually check on what changed, the status command is great to see what git thinks is going on so you can correct it.

```
```python
git status
git commit -a -m "A simple GET request handler."
git push