This is a module to interact with Server and Client
Project description
Socket and SSL
Create crt and key files for server and client
Here are the steps to create a self-signed SSL/TLS certificate and private key for a server:
Generate a private key for the server:
openssl genrsa -out server.key 2048
Generate a certificate signing request (CSR) for the server:
openssl req -new -key server.key -out server.csr
Generate a self-signed SSL/TLS certificate for the server:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Here are the steps to create a self-signed SSL/TLS certificate and private key for a client: Generate a private key for the client:
openssl genrsa -out client.key 2048
Generate a certificate signing request (CSR) for the client:
openssl req -new -key client.key -out client.csr
Generate a self-signed SSL/TLS certificate for the client:
openssl x509 -req -days 365 -in client.csr -signkey client.key -out client.crt```
## share certificate files between the server and client
Sure, here are the codes for each step:
Create a new file called chain.pem and open it in a text editor:
with open('chain.pem', 'w') as f: pass ``` Copy the contents of the client's SSL/TLS certificate (client.crt) into the chain.pem file:
with open('client.crt', 'r') as client_cert_file, open('chain.pem', 'a') as chain_file:
client_cert_contents = client_cert_file.read()
chain_file.write(client_cert_contents)
Copy the contents of the server's SSL/TLS certificate (server.crt) into the chain.pem file, below the client's certificate:
with open('server.crt', 'r') as server_cert_file, open('chain.pem', 'a') as chain_file:
server_cert_contents = server_cert_file.read()
chain_file.write(server_cert_contents)
Save and close the chain.pem file:
Note that you can combine steps 2 and 3 into a single block of code, like this:
with open('client.crt', 'r') as client_cert_file, open('server.crt', 'r') as server_cert_file, open('chain.pem', 'w') as chain_file:
client_cert_contents = client_cert_file.read()
server_cert_contents = server_cert_file.read()
chain_file.write(client_cert_contents + server_cert_contents)
This code block opens both the client.crt and server.crt files, reads their contents, and writes them to the chain.pem file.
codes for server and client
For Server:
import ssl
# Create an SSL context and load the server's certificate and private key
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile='server.crt', keyfile='server.key')
# Load the certificate chain file
ssl_context.load_verify_locations(cafile='chain.pem')
For Client
import ssl
# Create an SSL context and load the client's certificate and private key
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.load_cert_chain(certfile='client.crt', keyfile='client.key')
# Load the CA file as a trusted root CA
ssl_context.load_verify_locations(cafile='ca.crt')
# Create a socket and connect to the server
ssl_sock = ssl_context.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
ssl_sock.connect((HOST, PORT))
# Perform the SSL/TLS handshake and send data
ssl_sock.sendall(b'Hello, server!')
data = ssl_sock.recv(1024)
# Close the SSL/TLS connection
ssl_sock.close()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file JohnServerAPI-0.0.1.tar.gz
.
File metadata
- Download URL: JohnServerAPI-0.0.1.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a57ad2c67c901ae87e15110d817ab94e8440de446ca862ab8c475bf5d8966934 |
|
MD5 | 821387f3c6bc03b3a2da23b05fd1d04b |
|
BLAKE2b-256 | 301169429ffbce9e0016eeefd5c409814168abf26d68a379254e837e97046ea4 |
File details
Details for the file JohnServerAPI-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: JohnServerAPI-0.0.1-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9979f42a3cee647377232393a5f531c27bad3665c6c5f3212c1ba62983ca5377 |
|
MD5 | dceef02d8182c2644ebcbd246a591c94 |
|
BLAKE2b-256 | 8fd4e70289486f116f2cec9e29ab3c60e5204342536d2476c74160264b0e66ed |