webapp2 pgoram!Client computers generate a large time_wait_connect!

34 views
Skip to first unread message

bruce lee

unread,
Mar 23, 2017, 9:13:50 AM3/23/17
to Google App Engine
I use GAE platform to forward HTTP SSL data.(python webapp2)
why?use ssl client make large time_wait connect?
But HTTP is normal.




code:


#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import base64, logging
import webapp2
from google.appengine.api import urlfetch

class MainHandler(webapp2.RequestHandler):
    def get(self):
        useSSL = 0
        _url = self.request.url.lower()
        if _url.find('https://')==0:
            useSSL = 1

        _url = self.request.url #restore
        
        _path = self.request.path
        
        if _path=="/": #for test
            self.response.headers["Content-Type"] = "text/html; charset=utf-8"
            self.response.write("Hello, guys")
        else:      
            nPos = _url.find(_path)    
            sub_url = _url[nPos:len(_url)]

            if useSSL==1:
                url = "https://198.52.119.103:443"+sub_url
            else:
                url = "http://198.52.119.103"+sub_url

            rpc = urlfetch.create_rpc()
            urlfetch.make_fetch_call(rpc, url, validate_certificate=False)


            # ... do other things ..
            try:
                result = rpc.get_result()
                if result.status_code == 200:
                    text = result.content
                    self.response.write(text)
                else:
                    self.response.status_int = result.status_code
                    self.response.write('URL returned status code {}'.format(result.status_code))
            except urlfetch.DownloadError:
                self.response.status = 500
                self.response.write('Error fetching URL')

    def post(self):
        useSSL = 0
        _url = self.request.url.lower()
        if _url.find('https://')==0:
            useSSL = 1

        _url = self.request.url #restore
        
        _path = self.request.path
        
        if _path=="/": #for test
            self.response.headers["Content-Type"] = "text/html; charset=utf-8"
            self.response.write("Hello, guys")
        else:      
            nPos = _url.find(_path)    
            sub_url = _url[nPos:len(_url)]
    

            if useSSL==1:
                url = "https://test.server.com:443"+sub_url
            else:
                url = "http://test.server.com"+sub_url

            
                #def make_fetch_call(rpc, url, payload=None, method=GET, headers={},
                #    allow_truncated=False, follow_redirects=True,
                #    validate_certificate=None):

            _headers = {'Content-Type': 'application/octet-stream', 'Content-Length': self.request.headers['Content-Length']}
            rpc = urlfetch.create_rpc()
            urlfetch.make_fetch_call(rpc, url, self.request.body, 2, _headers, validate_certificate=False)


            # ... do other things ..
            try:
                result = rpc.get_result()
                if result.status_code == 200:
                    text = result.content
                    self.response.write(text)
                else:
                    self.response.status_int = result.status_code
                    self.response.write('URL returned status code {}'.format(result.status_code))
            except urlfetch.DownloadError:
                self.response.status = 500
                self.response.write('Error fetching URL')
      
app = webapp2.WSGIApplication([
    ('.*', MainHandler)
], debug=True)

Adam (Cloud Platform Support)

unread,
Mar 26, 2017, 12:42:31 PM3/26/17
to Google App Engine
I'm not sure why you'd want to do this, except maybe to hide the fact that your site does not have a valid SSL certificate. One cause of latency is the fact that you're buffering the entire response in App Engine before sending it along to the client, but really what you're doing is a hack and not good practice. You can get a real cert for free at https://letsencrypt.org and save yourself the trouble. 
Reply all
Reply to author
Forward
0 new messages