码迷,mamicode.com
首页 > 编程语言 > 详细

PYTHON的CGIServer的进化

时间:2015-11-25 10:58:55      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

按例程,一步一步理解如何从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的进化

标签:

原文地址:http://www.cnblogs.com/aguncn/p/4993774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!