PYTHON的CGIServer的进化
按例程,一步一步理解如何从SOCKET,TCP,HTTP,CGIHTTP进化的。
最终,静态文件和脚本分享,且能处理FORM提交和展示。
下一步,到数据库??:)
A,SOCKET
#HTTPserver
import socket
HOST = ''
PORT = 8088
text_content = '''
HTTP/1.x 200 OK
Content-Type: text/html
<html>
<head>
<title>WOW</title>
</head>
<body>
<p>WOW, Python Server</p>
<form name='input', action='/' method='post'>
First name :<input type='text' name='firstname'><br>
<input type='submit' value='submit'>
</form>
</body>
</html>
'''
f = open('hello.jpg', 'rb')
pic_content = '''
HTTP/1.1 200 OK
Content-Type: image/jpeg
'''
pic_content = pic_content + f.read()
f.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
print 'HTTPserver is start listen...'
while True:
s.listen(3)
conn, addr = s.accept()
request = conn.recv(1024)
print request.split(' ')
method = request.split(' ')[0]
src = request.split(' ')[1]
if method == 'GET':
if src == '/hello.jpg':
content = pic_content
else:
content = text_content
print 'Connetcted by', addr
print 'Request is:', request
conn.sendall(content)
if method == 'POST':
form = request.split('\r\n')
idx = form.index('')
entry = form[idx:]
value = entry[-1].split('=')[-1]
conn.sendall(text_content + '\n <p>' + value + '</p>')
conn.close()
B,TCP
#HTTPserver
#import socket
import SocketServer
HOST = ''
PORT = 8080
text_content = '''
HTTP/1.x 200 OK
Content-Type: text/html
<html>
<head>
<title>WOW</title>
</head>
<body>
<p>WOW, Python Server</p>
<form name='input', action='/' method='post'>
First name :<input type='text' name='firstname'><br>
<input type='submit' value='submit'>
</form>
</body>
</html>
'''
f = open('hello.jpg', 'rb')
pic_content = '''
HTTP/1.1 200 OK
Content-Type: image/jpeg
'''
pic_content = pic_content + f.read()
f.close()
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
print 'HTTPserver is start listen...'
request = self.request.recv(1024)
print 'Connetcted by', self.client_address[0]
print 'Request is:', request
method = request.split(' ')[0]
src = request.split(' ')[0]
if method == 'GET':
if src == '/hello.jpg':
content = pic_content
else:
content = text_content
self.request.sendall(content)
if method == 'POST':
form = request.split('\r\n')
idx = form.index('')
entry = form[idx:]
value = entry[-1].split('=')[-1]
self.request.sendall(text_content + '\n <p>' + value + '</p>')
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
C,HTTP
#HTTPserver #import socket import SocketServer import SimpleHTTPServer HOST = '' PORT = 8088 server = SocketServer.TCPServer((HOST, PORT), SimpleHTTPServer.SimpleHTTPRequestHandler) server.serve_forever()
D,CGI
#HTTPserver #import socket import BaseHTTPServer import CGIHTTPServer HOST = '' PORT = 8088 server = BaseHTTPServer.HTTPServer((HOST, PORT), CGIHTTPServer.CGIHTTPRequestHandler) server.serve_forever()
(HTML及PY)
<head> <title>WOW</title> </head> <html> <p>Wow, Python Server</p> <IMG src="hello.jpg"/> <form name="input" action="cgi-bin/post.py" method="post"> First name:<input type="text" name="firstname"><br> <input type="submit" value="Submit"> </form> </html> ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ import cgi form = cgi.FieldStorage() print 'Content-Type: text/html' print print '<p>Hello world!</p>' print '<p>' + repr(form['firstname']) + '</p>'
截图:


PYTHON的CGIServer的进化的更多相关文章
- Python 参数校验的进化
Python 函数参数魔法 事情的起因是感觉目前项目中的参数校验方法写的太简单了,很多时候需要在server层再if else处理,于是就动手准备写一个好用一点的,可以自定义校验参数规则的参数校验器, ...
- 【大爽python算法】递归算法进化之回溯算法(backtracking)
作者自我介绍:大爽歌, b站小UP主 , python1对1辅导老师, 时常直播编程,直播时免费回答简单问题. 前置知识: 递归算法(recursion algorithm). 我的递归教程: [教程 ...
- 【Python Deap库】遗传算法/遗传编程 进化算法基于python DEAP库深度解析讲解
目录 前言 概述 启发式的理解(重点) 优化问题的定义 个体编码 初始族群的创建 评价 配种选择 锦标赛 轮盘赌选择 随机普遍抽样选择 变异 单点交叉 两点交叉 均匀交叉 部分匹配交叉 突变 高斯突变 ...
- python之阶乘的小例子
现在自己写阶乘是这个样子的 def f(x): return x * f(x-1) if x >1 else 1 后来无意中看到耗子的一篇<Python程序员的进化>的文章, 感脚这 ...
- Python协程与JavaScript协程的对比
前言 以前没怎么接触前端对JavaScript 的异步操作不了解,现在有了点了解一查,发现 python 和 JavaScript 的协程发展史简直就是一毛一样! 这里大致做下横向对比和总结,便于对这 ...
- Python网络02 Python服务器进化
原文:Python网络02 Python服务器进化 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! **注意,在Python 3. ...
- geatpy - 遗传和进化算法相关算子的库函数(python)
Geatpy The Genetic and Evolutionary Algorithm Toolbox for Python Introduction Website (including doc ...
- Python遗传和进化算法框架(一)Geatpy快速入门
https://blog.csdn.net/qq_33353186/article/details/82014986 Geatpy是一个高性能的Python遗传算法库以及开放式进化算法框架,由华南理工 ...
- 《Python测试开发技术栈—巴哥职场进化记》—前言
写在前面 今年从4月份开始写一本讲Python测试开发技术栈的书,主要有两个目的,第一是将自己掌握的一些内容分享给大家,第二是希望自己能系统的梳理和学习Python相关的技术栈.当时我本来打算以故事体 ...
随机推荐
- js 金额格式化
//格式化金额 function fmoney(s, n) { n = n > 0 && n <= 20 ? n : 2; s = parseFloat((s + &quo ...
- windows2003可用gt630显卡驱动
http://file2.mydrivers.com/display/301.42-desktop-winxp-32-international-whql.exe 驱动精灵自动下载的不好用,这个版本可 ...
- Sublime Text3一些安装和使用技巧
ST3是一款很好的编辑软件,他不仅仅是能编辑前端代码,包括JS,PHP,HTML,CSS等,还能编辑JAVA,C++等常用后代编辑语言.因为本人写前端,本篇文章只介绍ST3的一些前端的技巧. 对于ST ...
- time 函数
1.Python Time 2.C++ Time Example: #include <iostream> #include <chrono> //C++的`计时`库 #inc ...
- attempt to write a readonly database 的解决办法
这个问题导致我的unity项目崩溃,以至于无法打开. 第一次出现这个问题是因为在Lighting窗口中build按钮下点击了clear all baked datas,导致unity强制退出,并给出上 ...
- Linux 网络I/O模型
前言 本文是笔者的第一篇博文,在这篇文章的大部分内容基于steven大神的<Unix Network Programming>.一来是对书本内容的整理与归纳.二来也是为接下来的博文奠定基础 ...
- c#拖放
AllowDrop DragEnter: if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Cop ...
- 出力csv
public static void ExportResultLog(System.Data.DataTable dt, string fileName, string path) { if (!Sy ...
- asp.net上传Excel文件到服务端进行读取
1.我们IIS是使用7.5,由于在网站中上传Excel文件到服务端进行数据读取时候出现读取失败情况.一开始以为是没有按照office软件问题,其实不然,因为server是64位操作系统,如果我们要使用 ...
- prefixfree.js介绍
假如你现在正学习着强大的Css3,你知道Css3的很多数属性为实验属性,使用他们的时候得加上各式各样的浏览器前缀.可能你默默忍受了,因为还没到统一的时间.有没想过给自己减下负,偶然间在网上看到一个js ...