按例程,一步一步理解如何从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. Smarty中一些标签的使用

    Smarty中的标签和php中的标签不一样 foreach标签{foreach   from=$goods(变量名) key='键,不带$' item='值,不带$'}中间的显示内容{/foreach ...

  2. Spark Shuffle实现

    Apache Spark探秘:Spark Shuffle实现 http://dongxicheng.org/framework-on-yarn/apache-spark-shuffle-details ...

  3. lookup:ID列

    对lookup列对应的ID列的引用的写法 if (item["NavType_x003a_ID"].ToString() == type["ID"].ToStr ...

  4. 2 WPF之XMAL----XMAL概览

    转载:http://blog.csdn.net/fwj380891124/article/details/8085458 微软为了把开发模式从网络开发移植到桌面开发和富媒体网络程序的开发上,微软创造了 ...

  5. gwt-问题解决

    最近在看gwt,写了个demo,但是总是出问题,困扰了好几天,后台也没报错,但就是加载不出来 第一次编译以后是可以的,但是改了代码后就不行了,后台也没报错,google了好长时间也没出来. 于是换了个 ...

  6. Android笔记之adb命令应用实例1(手机端与PC端socket通讯上)

    Android端的代码: 布局文件:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/ ...

  7. Dorado浏览器调试

    通常在项目中我们对js脚本进行调试有以下2种方式: alert调试法 首先是最原始也是最简单的使用alert,在页面中需要输出需要的变量的地方加上alert函数,将变量弹出显示:alert方式虽然简单 ...

  8. c++ primer (5)2

    第三章 1.头文件不应包含using声明,因为头文件的内容会拷贝到所有引用它的文件中去. 2.初始化string对象的方式: string s1; //默认初始化,s1是一个空串 string s2( ...

  9. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  10. Newtonsoft.Json.dll反序列化JSON字符串的方法

      1.直接反序列化JSON字符串 //引用序列化.反序列化JSON字符串用到的空间 using Newtonsoft.Json; using Newtonsoft.Json.Linq; //定义一个 ...