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相关的技术栈.当时我本来打算以故事体 ...
随机推荐
- HTML5 服务器发送事件
单向传输:服务器端——>客户端 作用:传回的能每过3s重新刷新一遍.从而能过跟数据库同步,与ajax配合使用 一.客户端写法 必须的用 message 方法 JSON.parse() ...
- 认识JSON
JSON 全称 JavaScript Object Notation,意思是JavaScript对象表示法.是一种基于文本的.独立于语言的轻量级数据交换格式.易于阅读和编写,易于机器解析和生成. 一. ...
- 【转】prototype扩展的JavaScript常用函数库
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- Ubuntu Update-rc.d命令详细介绍
http://www.jb51.net/os/Ubuntu/182768.html Ubuntu或者Debian系统中update-rc.d命令,是用来更新系统启动项的脚本.这些脚本的链接位于/etc ...
- CPrintDialog
CPrintDialog 封装windows为打印提供服务的通用窗体. BOOL GetDefaults(); //获取默认设备,不显示对话框 // Helpers for parsing infor ...
- StrHelper
public class StrHelper { private static string passWord; //加密字符串 /// <summary> /// 判断输入是否数字 // ...
- Dubbo在Spring和Spring Boot中的使用
一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...
- intellij idea社区版 & maven & git & tomcat/jetty 的struts2项目的搭建
1.新建一个project,并在project下新建一个maven module.
- Nosql_笔记
Nosql: http://www.infoq.com/cn/news/2011/01/nosql-why/ Redis: http://www.jb51.net/article/59294.htm ...
- 我爱工程化 之 gulp 使用(二)
上一篇 介绍了gulp的安装.环境等配置.基本使用,那么现在,我们快走进 速8,深入了解吧...... 一.各种安装.环境配置.插件安装(参考上一篇文章) 二.项目基本目录结构 三.编写 gulpf ...