mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-02-09 13:15:14 +08:00
add new examples
This commit is contained in:
57
src/30-openssl/ssl_client_server/server.py
Executable file
57
src/30-openssl/ssl_client_server/server.py
Executable file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Copyright 2018- The Pixie Authors.
|
||||
#
|
||||
# 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
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import socket
|
||||
import ssl
|
||||
import time
|
||||
import random
|
||||
|
||||
listen_addr = '127.0.0.1'
|
||||
listen_port = 8082
|
||||
server_cert = 'server.crt'
|
||||
server_key = 'server.key'
|
||||
client_certs = 'client.crt'
|
||||
|
||||
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
|
||||
context.verify_mode = ssl.CERT_REQUIRED
|
||||
context.load_cert_chain(certfile=server_cert, keyfile=server_key)
|
||||
context.load_verify_locations(cafile=client_certs)
|
||||
|
||||
bindsocket = socket.socket()
|
||||
bindsocket.bind((listen_addr, listen_port))
|
||||
bindsocket.listen(5)
|
||||
|
||||
while True:
|
||||
print("Waiting for client")
|
||||
newsocket, fromaddr = bindsocket.accept()
|
||||
print("Client connected: {}:{}".format(fromaddr[0], fromaddr[1]))
|
||||
conn = context.wrap_socket(newsocket, server_side=True)
|
||||
print("SSL established.")
|
||||
|
||||
count = 0
|
||||
while True:
|
||||
time.sleep(1)
|
||||
data = conn.recv(1024)
|
||||
print(data.decode())
|
||||
secret = random.randint(0, 1024 * 1024 * 1024)
|
||||
conn.send("Server secret {} is {}".format(count, secret).encode())
|
||||
count += 1
|
||||
|
||||
print("Closing connection")
|
||||
conn.shutdown(socket.SHUT_RDWR)
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user