按例程,一步一步理解如何从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的进化的更多相关文章

  1. Python 参数校验的进化

    Python 函数参数魔法 事情的起因是感觉目前项目中的参数校验方法写的太简单了,很多时候需要在server层再if else处理,于是就动手准备写一个好用一点的,可以自定义校验参数规则的参数校验器, ...

  2. 【大爽python算法】递归算法进化之回溯算法(backtracking)

    作者自我介绍:大爽歌, b站小UP主 , python1对1辅导老师, 时常直播编程,直播时免费回答简单问题. 前置知识: 递归算法(recursion algorithm). 我的递归教程: [教程 ...

  3. 【Python Deap库】遗传算法/遗传编程 进化算法基于python DEAP库深度解析讲解

    目录 前言 概述 启发式的理解(重点) 优化问题的定义 个体编码 初始族群的创建 评价 配种选择 锦标赛 轮盘赌选择 随机普遍抽样选择 变异 单点交叉 两点交叉 均匀交叉 部分匹配交叉 突变 高斯突变 ...

  4. python之阶乘的小例子

    现在自己写阶乘是这个样子的 def f(x): return x * f(x-1) if x >1 else 1 后来无意中看到耗子的一篇<Python程序员的进化>的文章, 感脚这 ...

  5. Python协程与JavaScript协程的对比

    前言 以前没怎么接触前端对JavaScript 的异步操作不了解,现在有了点了解一查,发现 python 和 JavaScript 的协程发展史简直就是一毛一样! 这里大致做下横向对比和总结,便于对这 ...

  6. Python网络02 Python服务器进化

    原文:Python网络02 Python服务器进化 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! **注意,在Python 3. ...

  7. geatpy - 遗传和进化算法相关算子的库函数(python)

    Geatpy The Genetic and Evolutionary Algorithm Toolbox for Python Introduction Website (including doc ...

  8. Python遗传和进化算法框架(一)Geatpy快速入门

    https://blog.csdn.net/qq_33353186/article/details/82014986 Geatpy是一个高性能的Python遗传算法库以及开放式进化算法框架,由华南理工 ...

  9. 《Python测试开发技术栈—巴哥职场进化记》—前言

    写在前面 今年从4月份开始写一本讲Python测试开发技术栈的书,主要有两个目的,第一是将自己掌握的一些内容分享给大家,第二是希望自己能系统的梳理和学习Python相关的技术栈.当时我本来打算以故事体 ...

随机推荐

  1. 【C#4.0图解教程】笔记(第9章~第18章)

    第9章 语句 1.标签语句 ①.标签语句由一个标识符后面跟着一个冒号再跟着一条语句组成 ②.标签语句的执行完全如同标签不存在一样,并仅执行冒号后的语句. ③.给语句添加一个标签允许控制从代码的另一部分 ...

  2. Sqlserver 快照

    最近,开发系统使用SqlServer2008 R2,但是由于系统数据压力的增加,准备增加一个和正式数据库同步的库,用来供接口和报表使用,所以开始对SqlServer里面的一些技术开始研究,第一篇先来研 ...

  3. EF架构~过滤导航属性等,拼接SQL字符串

    拼接T-SQL串,并使它具有通用性 好处:与服务器建立一次连接,给服务器发一条SQL命令,即可实现 代码如下: 1 /// <summary> 2 /// 构建Insert语句串 3 // ...

  4. Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法

    Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法 Summary的用法和Group一样简单,分为两步: 启用Summary功能 在Feature标签内,添加如 ...

  5. 第九篇、CSS布局

    <!--css布局 标准流:从上到下 从左到右 脱离标准流:(浮在父控件的最左边或者最右边)(类似ios在window上添加的控件) 1.float: 2.position: absolute( ...

  6. UICollectionView的简单使用

    ChildModel.h #import <Foundation/Foundation.h> @interface ChildModel : NSObject @property (non ...

  7. Objective-C 学习笔记(Day 2)

    ------------------------------------------- 如何根据题目准确完整清晰的声明一个类并实现给定的行为 /*  //下面这个程序教大家如何根据题目去声明一个类,并 ...

  8. javascript form 第22节

    <html> <head> <title>Form对象</title> </style> <script type="tex ...

  9. ajax和jsonp的封装

    一直在用jQuery的ajax,跨域也是一直用的jQuery的jsonp,jQuery确实很方便,$.ajax({...})就可以搞定. 为了更好的理解ajax和jsonp,又重新看了下书,看了一些博 ...

  10. Headfirst设计模式的C++实现——外观模式(Facade)

    light.h #ifndef _LIGHT_H_ #define _LIGHT_H_ #include <iostream> class LIGHT { public: void dim ...