论坛首页 编程语言技术论坛

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

浏览 2090 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-03-12  

最近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似乎不能根据内容转发。。不会。

 

 

   发表时间:2010-03-13  
不过这段代码在35行的recv经常阻塞,搞不定。!!!!

有时返回数据为空,有时只有1行!
有时被阻塞,难道是jetty server_file没有关闭流???
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics