test_server.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # Copyright 2015 gRPC authors.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Server for httpcli_test"""
16 
17 import argparse
18 from http.server import BaseHTTPRequestHandler
19 from http.server import HTTPServer
20 import os
21 import ssl
22 import sys
23 
24 _PEM = os.path.abspath(
25  os.path.join(os.path.dirname(sys.argv[0]), '../../..',
26  'src/core/tsi/test_creds/server1.pem'))
27 _KEY = os.path.abspath(
28  os.path.join(os.path.dirname(sys.argv[0]), '../../..',
29  'src/core/tsi/test_creds/server1.key'))
30 print(_PEM)
31 open(_PEM).close()
32 
33 argp = argparse.ArgumentParser(description='Server for httpcli_test')
34 argp.add_argument('-p', '--port', default=10080, type=int)
35 argp.add_argument('-s', '--ssl', default=False, action='store_true')
36 args = argp.parse_args()
37 
38 print('server running on port %d' % args.port)
39 
40 
41 class Handler(BaseHTTPRequestHandler):
42 
43  def good(self):
44  self.send_response(200)
45  self.send_header('Content-Type', 'text/html')
46  self.end_headers()
47  self.wfile.write(
48  '<html><head><title>Hello world!</title></head>'.encode('ascii'))
49  self.wfile.write(
50  '<body><p>This is a test</p></body></html>'.encode('ascii'))
51 
52  def do_GET(self):
53  if self.path == '/get':
54  self.good()
55 
56  def do_POST(self):
57  content_len = self.headers.get('content-length')
58  content = self.rfile.read(int(content_len)).decode('ascii')
59  if self.path == '/post' and content == 'hello':
60  self.good()
61 
62 
63 httpd = HTTPServer(('localhost', args.port), Handler)
64 if args.ssl:
65  httpd.socket = ssl.wrap_socket(httpd.socket,
66  certfile=_PEM,
67  keyfile=_KEY,
68  server_side=True)
69 httpd.serve_forever()
get
absl::string_view get(const Cont &c)
Definition: abseil-cpp/absl/strings/str_replace_test.cc:185
test_server.Handler
Definition: test_server.py:41
write
#define write
Definition: test-fs.c:47
grpc._common.encode
def encode(s)
Definition: grpc/_common.py:68
test_server.Handler.path
path
Definition: test_server.py:53
xds_interop_client.int
int
Definition: xds_interop_client.py:113
close
#define close
Definition: test-fs.c:48
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
grpc._common.decode
def decode(b)
Definition: grpc/_common.py:75
open
#define open
Definition: test-fs.c:46
test_server.Handler.do_POST
def do_POST(self)
Definition: test_server.py:56
test_server.Handler.good
def good(self)
Definition: test_server.py:43
test_server.Handler.do_GET
def do_GET(self)
Definition: test_server.py:52


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:31