GWT & json方式时,用代理服务器 对付同源约束

最近GWT + GAE python

..至于为什么用了GAE python?当然用GAE java 代码管理更方便,但是有PyFetion,没有好的JavaFetion...

 

文档上说:因为同源约束,所以需要每次把gwt编译后拷贝到 服务器环境中,

于是,来了段代码:

 

GAE 跑在8080端口

GWT在8888端口

写一个代理服务器,监听7777端口,如果是GAE的请求转发到8080,否则转发到8888

 

 

import SocketServer
import socket

GAE_PORT = 8080
GWT_PORT = 8888

gwt_ext = ['html',
           'js',
           'png',
           'css',
]

def is_gwt(data):
    tmp = data [0: data.find('HTTP/1')]
    for ext in gwt_ext: 
        if (tmp.find(ext) > 0):
            return True
    return False


def ning_request(data):

    if is_gwt(data):
        print "ning: gwt\n\n"
        PORT = GWT_PORT
    else : 
        print "ning: gae\n\n"
        PORT = GAE_PORT

    HOST = "localhost"
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((HOST, PORT))
    sock.send(data)

    received = ''
    while True:
        line = sock.recv(1024)
        received += line
        if len(line) < 1024: # Zero length indicates EOF
            break
#    received = sock.recv(1024*1024)
    print "ning:", data
    print "ning:", received
    return received

class MyTCPHandler(SocketServer.StreamRequestHandler):
    def handle(self):
#        received = self.request.recv(1024*1024)

        received = ''
        while True:
            line = self.request.recv(1024)
            print "ning:line:", line
            received += line
            if len(line) < 1024: # Zero length indicates EOF
                break

#        print self.data
        self.wfile.write(ning_request(received))
#        self.rfile.close()
#        self.wfile.close()

if __name__ == "__main__":
    HOST, PORT = "localhost", 7777

    # Create the server, binding to localhost on port 9999
    server = SocketServer.ThreadingTCPServer((HOST, PORT), MyTCPHandler)
#    server = SocketServer.ThreadingTCPServer(100, server)
    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    print "listening on :" , PORT
    server.serve_forever()

 

颇费了一点周折,因为开始想用nc直接搞,但是nc似乎不能根据内容转发。。不会。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值