登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> 程序员学前班[不再更新,只读] >> 主题: Making a simple web server in Python     [回主站]     [分站链接]
标题
Making a simple web server in Python
clq
浏览(1) + 2010-05-06 00:31:03 发表 编辑

关键字:

Making a simple web server in Python

http://fragments.turtlemeat.com/pythonwebserver.php

--------------------------------------------------

written by Jon Berg <jon.berg[at]turtlemeat.com>

The great thing about Python is what they call "batteries included", that means a lot of functionallity is bundled with the programming language. And the functionallity is fairly straight to use.

Today we are going to demonstrate how to build a web server in less than 30 min. using the classes from the Python library. We are also going to have some "advanced" functionallity in this server. We want to do dynamic content serving (ala what ASP or PHP do), and be able to take POST from forms. This may sound as a complex task, but as you will see every body can acomplish this through some high level programming interfaces. You don't have to know much about the HTTP protocol at all. Except some basic that when the client request something it is a "GET", and when the client sends something it is in our case a POST. Some basic responce codes like 200 is OK for GET, and 404 is file not found, 301 is OK for a Post.

The web server is implemented as an extention of the BaseHTTPRequestHandler. This means that we define a class an implemenet some methods that will be called when the web server gets requests. There are two methods one for GET requests and one for POST requests.

Lets look at the code:


The main() tries to start the server at port 80 with a reference to the class we just implemented. To quit the server you can press ctrl-c. And the server will close the socket and quit.

The do_GET() gets invoked when you do a GET request. It do checking to see what type of file is requested. If it is a html file it tries to open it and send it to the client. If it is a esp file, our dynamic content, it will print out the current day and year.

The do_POST() gets invoked when you do a POST request. It then displays the content that is uploaded. The name of the formelement is hardcoded to be "upfile".

Running the example:
start the webserver with python webserver.py
Then you can open your web browser and type in http://localhost/index.html or http://localhost/index.esp
To test the upload, you open the upload.html, press browse, open a small text file, click "press", the server will echo back the file.

Download the example code. (webserver.py, index.html, upload.html)


Pointers to related stuff on the hypertext transfer protocol (http) and web servers:

Making a simple web server in Java. (a bit lower level) This is a tutorial I did on how to do somewhat the same but in Java. This implementation is much more bottom up, with sockets and not that many short cuts as you can do in Python.

HowStuffWorks This is a very basic introduction for the novice on how web servers works. Has also other text about Internet and Routers, in easy to understandable language. Nice if you are starting out learning.

HTTP Made Really Easy Here is some nice text that goes through the main points in HTTP. A little more technical. It's recommended that you know these things to understand the above tutorial.

Apache the best and most widely used web server on the Internet today, check it out. If you want to run your own web server this is the one to get, you can get binaries for both Windows and Unix. You can download the entire sourcecode if you want to check how it was made.

Mozilla / Netscape is a nice web browser. Get rid of the Explorer.

RFC-1945 RFC describing Hypertext Transfer Protocol -- HTTP/1.0

RFC-2616 RFC describing Hypertext Transfer Protocol -- HTTP/1.1

RFC webpage The Request For Comment webpage, RFC are technical documents describing all sorts of protocols on the Internet.

Python webpage Download Python for windows or linux.




总数:0 页次:1/0 首页 尾页  
总数:0 页次:1/0 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1