Fix portability (#28284)

* Add python3 to list of acceptable pythons in python wrapper

* Attempt to make fake server python3 compatible

* py3 fixes for test server

* more py3 fixes for test server

* require python3 for test/core/http/test_server.py

Co-authored-by: Alexander Polcyn <apolcyn@google.com>
pull/28308/head
Jan Tattermusch 3 years ago committed by GitHub
parent 5056fe81bd
commit e0b3582701
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      test/core/http/test_server.py
  2. 2
      tools/distrib/python_wrapper.sh

@ -1,4 +1,4 @@
#!/usr/bin/env python2.7 #!/usr/bin/env python3
# Copyright 2015 gRPC authors. # Copyright 2015 gRPC authors.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
@ -15,19 +15,19 @@
"""Server for httpcli_test""" """Server for httpcli_test"""
import argparse import argparse
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer
import os import os
import ssl import ssl
import sys import sys
import BaseHTTPServer
_PEM = os.path.abspath( _PEM = os.path.abspath(
os.path.join(os.path.dirname(sys.argv[0]), '../../..', os.path.join(os.path.dirname(sys.argv[0]), '../../..',
'src/core/tsi/test_creds/server1.pem')) 'src/core/tsi/test_creds/server1.pem'))
_KEY = os.path.abspath( _KEY = os.path.abspath(
os.path.join(os.path.dirname(sys.argv[0]), '../../..', os.path.join(os.path.dirname(sys.argv[0]), '../../..',
'src/core/tsi/test_creds/server1.key')) 'src/core/tsi/test_creds/server1.key'))
print _PEM print(_PEM)
open(_PEM).close() open(_PEM).close()
argp = argparse.ArgumentParser(description='Server for httpcli_test') argp = argparse.ArgumentParser(description='Server for httpcli_test')
@ -35,29 +35,32 @@ argp.add_argument('-p', '--port', default=10080, type=int)
argp.add_argument('-s', '--ssl', default=False, action='store_true') argp.add_argument('-s', '--ssl', default=False, action='store_true')
args = argp.parse_args() args = argp.parse_args()
print 'server running on port %d' % args.port print('server running on port %d' % args.port)
class Handler(BaseHTTPServer.BaseHTTPRequestHandler): class Handler(BaseHTTPRequestHandler):
def good(self): def good(self):
self.send_response(200) self.send_response(200)
self.send_header('Content-Type', 'text/html') self.send_header('Content-Type', 'text/html')
self.end_headers() self.end_headers()
self.wfile.write('<html><head><title>Hello world!</title></head>') self.wfile.write(
self.wfile.write('<body><p>This is a test</p></body></html>') '<html><head><title>Hello world!</title></head>'.encode('ascii'))
self.wfile.write(
'<body><p>This is a test</p></body></html>'.encode('ascii'))
def do_GET(self): def do_GET(self):
if self.path == '/get': if self.path == '/get':
self.good() self.good()
def do_POST(self): def do_POST(self):
content = self.rfile.read(int(self.headers.getheader('content-length'))) content_len = self.headers.get('content-length')
content = self.rfile.read(int(content_len)).decode('ascii')
if self.path == '/post' and content == 'hello': if self.path == '/post' and content == 'hello':
self.good() self.good()
httpd = BaseHTTPServer.HTTPServer(('localhost', args.port), Handler) httpd = HTTPServer(('localhost', args.port), Handler)
if args.ssl: if args.ssl:
httpd.socket = ssl.wrap_socket(httpd.socket, httpd.socket = ssl.wrap_socket(httpd.socket,
certfile=_PEM, certfile=_PEM,

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
for p in python2.7 python2.6 python2 python not_found ; do for p in python3 python not_found ; do
python=$(which $p || echo not_found) python=$(which $p || echo not_found)

Loading…
Cancel
Save