python转发数据,做web代理

#-*- encoding:utf-8 -*-
import os
from sys import stdout,stderr
import cgi
import cgitb
import socket
import traceback

try:
    #cgitb.enable()
    stdout.write("Content-Type: text/plain; charset=utf-8\r\n")
    #这个Transfer-Encoding标记不需要,经试验apache默认设置content-length头的cgi自动为chunk模式,也就是说apache实现了自动分块传输,注意不要搞Content-Length啊
    #stdout.write("Transfer-Encoding: chunked\r\n")
    stdout.write("\r\n")
   
    if __name__ == '__main__':
        form = cgi.FieldStorage()
        dic = {}
        for key in form:
            dic[key] = form[key].value
        #
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(("192.168.56.23", 8086))
            sock.send("data")
            received = sock.recv(1024)
            stdout.write(received)
        finally:
            sock.close()
        #
    #
except:
    stdout.write(traceback.format_exc())     
#

你可能感兴趣的:(C++/Python/java)