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>'
复制代码

截图:

目录
相关文章
|
JSON 数据格式 Python
Python 参数校验的进化
事情的起因是感觉目前项目中的参数校验方法写的太简单了,很多时候需要在server层再if else处理,于是就动手准备写一个好用一点的,可以自定义校验参数规则的参数校验器,考虑到要可以灵活的配置就萌生了大概的印象: 使用map - 参数A:ruleA,参数B-ruleB.
2303 0
|
3月前
|
机器学习/深度学习 人工智能 算法
深入理解Python与机器学习的协同进化
在当今的技术驱动时代,Python已成为连接数据科学、人工智能(AI)和机器学习(ML)等领域的桥梁。本文旨在探索Python语言如何成为机器学习项目的首选语言,并详细分析了Python在这一领域的优势和挑战。通过对比其他编程语言,本文揭示了Python在机器学习应用中的独特地位,同时也指出了其面临的一些限制和未来发展方向。此外,文章还将探讨一些流行的Python机器学习库及其在实际项目中的应用案例,为读者提供全面而深入的视角,理解Python与机器学习的协同进化。
|
10月前
|
算法 调度 Python
改进的多目标差分进化算法在电力系统环境经济调度中的应用(Python代码实现)【电气期刊论文复现】
改进的多目标差分进化算法在电力系统环境经济调度中的应用(Python代码实现)【电气期刊论文复现】
|
1天前
|
网络协议 算法 网络架构
Python网络编程之udp编程、黏包以及解决方案、tcpserver
Python网络编程之udp编程、黏包以及解决方案、tcpserver
|
2天前
|
机器学习/深度学习 数据挖掘 算法框架/工具
Python:编程的艺术与魅力
Python:编程的艺术与魅力
10 3
|
4天前
|
机器学习/深度学习 数据挖掘 API
pymc,一个灵活的的 Python 概率编程库!
pymc,一个灵活的的 Python 概率编程库!
13 1
|
5天前
|
人工智能 算法 调度
uvloop,一个强大的 Python 异步IO编程库!
uvloop,一个强大的 Python 异步IO编程库!
17 2
|
5天前
|
机器学习/深度学习 人工智能 数据可视化
Python:探索编程之美
Python:探索编程之美
9 0
|
5天前
|
机器学习/深度学习 人工智能 数据处理
Python编程的魅力与实践
Python编程的魅力与实践