Name | JohnServerAPI JSON |
Version |
0.0.2
JSON |
| download |
home_page | |
Summary | This is a module to interact with Server and Client |
upload_time | 2023-03-20 01:38:02 |
maintainer | |
docs_url | None |
author | John |
requires_python | |
license | |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Title: John's Custom HTTP Client/Server Library
Description: This library provides a custom implementation of various network services and protocols such as REST, TCP, UDP, IP range calculation, and SSL/TLS. It offers the ability to interact with RESTful APIs, send/receive data over TCP or UDP, manage IP ranges, and secure data using SSL/TLS.
## Classes and Functions
JohnRESTServer
A class for interacting with RESTful APIs.
```
__init__(self): Initializes the class and sets the base URL for the RESTful API.
get_data(self, endpoint='objects', param=1): Sends a GET request to the specified endpoint and returns the data.
post_data(self, endpoint='objects', param=1, data={...}): Sends a POST request to the specified endpoint with the given data.
put_data(self, endpoint='objects', param=1): Sends a PUT request to the specified endpoint with the given data.
delete_data(self, endpoint='objects', param=1): Sends a DELETE request to the specified endpoint.
```
## JohnTCPServer
A class for handling TCP connections.
handle(self): Processes incoming data from the client and sends a response back.
JohnUDPServer
A class for handling UDP connections.
handle(self): Processes incoming data from the client and sends a response back.
JohnIPRange
A class for managing IP ranges.
ipd4range(self, cidr): Calculates and returns a list of IPv4 addresses within the given CIDR range.
ipd6range(self, cidr): Calculates and returns a list of IPv6 addresses within the given CIDR range.
process_command(command)
A function that processes incoming commands and returns a response.
JohnInterfaceHandler
A class for handling HTTP requests.
do_GET(self): Processes incoming GET requests and sends a response back.
JohnXMLRPCReqHandler
A class that inherits from SimpleXMLRPCRequestHandler.
JohnXMLRPCRequestHandler
A class for handling XML-RPC requests.
__init__(self, url='localhost', port=8000): Initializes the class and sets up the XML-RPC server.
start_server(self): Starts the XML-RPC server.
add(self, x, y): A sample XML-RPC function that adds two numbers and returns the result.
JohnAuthenticationHandler
A function for handling SSL/TLS connections. It accepts incoming connections and receives data.
ssl_client(msg, client_cert='ca.crt', host='localhost', port=12345)
A function that sends a message to an SSL/TLS server and receives a response.
Example Usage
```
from JohnServerAPI import JohnRESTServer, JohnTCPServer, JohnUDPServer, JohnIPRange, JohnInterfaceHandler, JohnXMLRPCRequestHandler, JohnAuthenticationHandler, ssl_client
# Interact with a RESTful API
rest_server = JohnRESTServer()
data = rest_server.get_data()
# Create and run a TCP server
tcp_server = socketserver.TCPServer(("", PORT), JohnTCPServer)
tcp_server.serve_forever()
# Create and run a UDP server
udp_server = socketserver.UDPServer(("", PORT), JohnUDPServer)
udp_server.serve_forever()
# Calculate IP ranges
ip_range = JohnIPRange()
ipv4_range = ip_range.ipd4range('123.45.67.64/27')
ipv6_range = ip_range.ipd6range('2001:db8::/64')
# Create and run an HTTP server
http_server = socketserver.TCPS
```
TestJohnServer
This is a test suite for the JohnServerAPI module. It contains multiple test cases to ensure that the different functionalities of the JohnServerAPI are working correctly.
Importing Dependencies
```
import unittest
import sys
sys.path.append('../1st')
import JohnServerAPI.JohnServerAPI as JSA
import threading
import socketserver
from http.server import HTTPServer
import json
import http.client
import multiprocessing
import ipaddress
```
# Test Class
The TestJohnServer class inherits from unittest.TestCase and contains several test methods for different functionalities provided by the JohnServerAPI.
```
class TestJohnServer(unittest.TestCase):
```
## setUp
The setUp method initializes the test environment and is executed before each test case.
```
def setUp(self):
self.rest_server = JSA.JohnRESTServer()
```
## test_rest_get_data
This test case checks if the get_data method of JohnRESTServer returns the expected data.
```
def test_rest_get_data(self):
```
## test_rest_post_data
This test case checks if the post_data method of JohnRESTServer returns the expected response.
```
def test_rest_post_data(self):
```
## test_rest_put_data
This test case checks if the put_data method of JohnRESTServer returns the expected response.
```
def test_rest_put_data(self):
```
## test_rest_delete_data
This test case checks if the delete_data method of JohnRESTServer returns the expected response.
```
def test_rest_delete_data(self):
```
## test_ipd4range
This test case checks if the ipd4range method of JohnIPRange returns the expected IP range.
```
def test_ipd4range(self):
```
## test_client_server_interaction
This test case checks if the client-server interaction using JohnInterfaceHandler works correctly.
```
def test_client_server_interaction(self):
```
## run_server
This static method starts a TCP server using JohnTCPServer.
```
@staticmethod
def run_server():
```
## test_ssl_authenticate
This test case checks if the SSL authentication using JohnAuthenticationHandler works correctly.
def test_ssl_authenticate(self):
## test_JohnXMLRPCRequestHandler
This test case checks if the JohnXMLRPCRequestHandler processes requests correctly.
```
def test_JohnXMLRPCRequestHandler(self):
```
## Running the Test Suite
The test suite is run using the unittest.main() function.
```
if __name__ == '__main__':
unittest.main()
```
This test suite helps ensure the correct functionality of the JohnServerAPI module by covering different aspects of the code.
# 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()
```
Raw data
{
"_id": null,
"home_page": "",
"name": "JohnServerAPI",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "John",
"author_email": "myemail@example.com",
"download_url": "https://files.pythonhosted.org/packages/39/f4/3a4b1edf84aa27427f755e26ad0092cac486706eed614a89b40807298d15/JohnServerAPI-0.0.2.tar.gz",
"platform": null,
"description": "# Title: John's Custom HTTP Client/Server Library\n\nDescription: This library provides a custom implementation of various network services and protocols such as REST, TCP, UDP, IP range calculation, and SSL/TLS. It offers the ability to interact with RESTful APIs, send/receive data over TCP or UDP, manage IP ranges, and secure data using SSL/TLS.\n\n## Classes and Functions\n\nJohnRESTServer\nA class for interacting with RESTful APIs.\n\n```\n__init__(self): Initializes the class and sets the base URL for the RESTful API.\nget_data(self, endpoint='objects', param=1): Sends a GET request to the specified endpoint and returns the data.\npost_data(self, endpoint='objects', param=1, data={...}): Sends a POST request to the specified endpoint with the given data.\nput_data(self, endpoint='objects', param=1): Sends a PUT request to the specified endpoint with the given data.\ndelete_data(self, endpoint='objects', param=1): Sends a DELETE request to the specified endpoint.\n```\n\n## JohnTCPServer\nA class for handling TCP connections.\n\nhandle(self): Processes incoming data from the client and sends a response back.\nJohnUDPServer\nA class for handling UDP connections.\n\nhandle(self): Processes incoming data from the client and sends a response back.\nJohnIPRange\nA class for managing IP ranges.\n\nipd4range(self, cidr): Calculates and returns a list of IPv4 addresses within the given CIDR range.\nipd6range(self, cidr): Calculates and returns a list of IPv6 addresses within the given CIDR range.\nprocess_command(command)\nA function that processes incoming commands and returns a response.\n\nJohnInterfaceHandler\nA class for handling HTTP requests.\n\ndo_GET(self): Processes incoming GET requests and sends a response back.\nJohnXMLRPCReqHandler\nA class that inherits from SimpleXMLRPCRequestHandler.\n\nJohnXMLRPCRequestHandler\nA class for handling XML-RPC requests.\n\n__init__(self, url='localhost', port=8000): Initializes the class and sets up the XML-RPC server.\nstart_server(self): Starts the XML-RPC server.\nadd(self, x, y): A sample XML-RPC function that adds two numbers and returns the result.\nJohnAuthenticationHandler\nA function for handling SSL/TLS connections. It accepts incoming connections and receives data.\n\nssl_client(msg, client_cert='ca.crt', host='localhost', port=12345)\nA function that sends a message to an SSL/TLS server and receives a response.\n\nExample Usage\n\n```\nfrom JohnServerAPI import JohnRESTServer, JohnTCPServer, JohnUDPServer, JohnIPRange, JohnInterfaceHandler, JohnXMLRPCRequestHandler, JohnAuthenticationHandler, ssl_client\n\n# Interact with a RESTful API\nrest_server = JohnRESTServer()\ndata = rest_server.get_data()\n\n# Create and run a TCP server\ntcp_server = socketserver.TCPServer((\"\", PORT), JohnTCPServer)\ntcp_server.serve_forever()\n\n# Create and run a UDP server\nudp_server = socketserver.UDPServer((\"\", PORT), JohnUDPServer)\nudp_server.serve_forever()\n\n# Calculate IP ranges\nip_range = JohnIPRange()\nipv4_range = ip_range.ipd4range('123.45.67.64/27')\nipv6_range = ip_range.ipd6range('2001:db8::/64')\n\n# Create and run an HTTP server\nhttp_server = socketserver.TCPS\n```\n\nTestJohnServer\nThis is a test suite for the JohnServerAPI module. It contains multiple test cases to ensure that the different functionalities of the JohnServerAPI are working correctly.\n\nImporting Dependencies\n```\nimport unittest\nimport sys\nsys.path.append('../1st')\nimport JohnServerAPI.JohnServerAPI as JSA\n\nimport threading\nimport socketserver\nfrom http.server import HTTPServer\nimport json\nimport http.client\n\nimport multiprocessing\nimport ipaddress\n```\n\n# Test Class\nThe TestJohnServer class inherits from unittest.TestCase and contains several test methods for different functionalities provided by the JohnServerAPI.\n\n```\nclass TestJohnServer(unittest.TestCase):\n```\n\n## setUp\nThe setUp method initializes the test environment and is executed before each test case.\n\n```\ndef setUp(self):\n self.rest_server = JSA.JohnRESTServer()\n```\n\n## test_rest_get_data\nThis test case checks if the get_data method of JohnRESTServer returns the expected data.\n\n```\ndef test_rest_get_data(self):\n```\n\n## test_rest_post_data\nThis test case checks if the post_data method of JohnRESTServer returns the expected response.\n\n```\ndef test_rest_post_data(self):\n```\n\n## test_rest_put_data\nThis test case checks if the put_data method of JohnRESTServer returns the expected response.\n\n```\ndef test_rest_put_data(self):\n```\n\n## test_rest_delete_data\nThis test case checks if the delete_data method of JohnRESTServer returns the expected response.\n\n```\ndef test_rest_delete_data(self):\n```\n\n## test_ipd4range\nThis test case checks if the ipd4range method of JohnIPRange returns the expected IP range.\n\n```\ndef test_ipd4range(self):\n```\n\n## test_client_server_interaction\nThis test case checks if the client-server interaction using JohnInterfaceHandler works correctly.\n\n```\ndef test_client_server_interaction(self):\n```\n\n## run_server\nThis static method starts a TCP server using JohnTCPServer.\n\n```\n@staticmethod\ndef run_server():\n```\n\n## test_ssl_authenticate\nThis test case checks if the SSL authentication using JohnAuthenticationHandler works correctly.\n\n\ndef test_ssl_authenticate(self):\n\n## test_JohnXMLRPCRequestHandler\nThis test case checks if the JohnXMLRPCRequestHandler processes requests correctly.\n\n\n```\ndef test_JohnXMLRPCRequestHandler(self):\n```\n\n## Running the Test Suite\nThe test suite is run using the unittest.main() function.\n\n```\nif __name__ == '__main__':\n unittest.main()\n```\n\nThis test suite helps ensure the correct functionality of the JohnServerAPI module by covering different aspects of the code.\n\n\n\n# Socket and SSL \n## Create crt and key files for server and client\nHere are the steps to create a self-signed SSL/TLS certificate and private key for a server:\n\nGenerate a private key for the server:\n\n\n```\nopenssl genrsa -out server.key 2048\n```\nGenerate a certificate signing request (CSR) for the server:\n\n```\nopenssl req -new -key server.key -out server.csr\n```\nGenerate a self-signed SSL/TLS certificate for the server:\n\n```\nopenssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt\n```\n\nHere are the steps to create a self-signed SSL/TLS certificate and private key for a client:\nGenerate a private key for the client:\n\n```\nopenssl genrsa -out client.key 2048\n```\nGenerate a certificate signing request (CSR) for the client:\n\n```\nopenssl req -new -key client.key -out client.csr\n```\nGenerate a self-signed SSL/TLS certificate for the client:\n\n```\nopenssl x509 -req -days 365 -in client.csr -signkey client.key -out client.crt```\n\n\n## share certificate files between the server and client\nSure, here are the codes for each step:\n\nCreate a new file called chain.pem and open it in a text editor:\n```\nwith open('chain.pem', 'w') as f:\n pass\n ```\nCopy the contents of the client's SSL/TLS certificate (client.crt) into the chain.pem file:\n```\nwith open('client.crt', 'r') as client_cert_file, open('chain.pem', 'a') as chain_file:\n client_cert_contents = client_cert_file.read()\n chain_file.write(client_cert_contents)\n```\nCopy the contents of the server's SSL/TLS certificate (server.crt) into the chain.pem file, below the client's certificate:\n```\nwith open('server.crt', 'r') as server_cert_file, open('chain.pem', 'a') as chain_file:\n server_cert_contents = server_cert_file.read()\n chain_file.write(server_cert_contents)\n```\nSave and close the chain.pem file:\n\n\nNote that you can combine steps 2 and 3 into a single block of code, like this:\n\n```\nwith open('client.crt', 'r') as client_cert_file, open('server.crt', 'r') as server_cert_file, open('chain.pem', 'w') as chain_file:\n client_cert_contents = client_cert_file.read()\n server_cert_contents = server_cert_file.read()\n chain_file.write(client_cert_contents + server_cert_contents)\n```\nThis code block opens both the client.crt and server.crt files, reads their contents, and writes them to the chain.pem file.\n\n## codes for server and client\n\nFor Server: \n\n```\nimport ssl\n\n# Create an SSL context and load the server's certificate and private key\nssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)\nssl_context.load_cert_chain(certfile='server.crt', keyfile='server.key')\n\n# Load the certificate chain file\nssl_context.load_verify_locations(cafile='chain.pem')\n```\nFor Client\n```\nimport ssl\n\n# Create an SSL context and load the client's certificate and private key\nssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)\nssl_context.load_cert_chain(certfile='client.crt', keyfile='client.key')\n\n# Load the CA file as a trusted root CA\nssl_context.load_verify_locations(cafile='ca.crt')\n\n# Create a socket and connect to the server\nssl_sock = ssl_context.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))\nssl_sock.connect((HOST, PORT))\n\n# Perform the SSL/TLS handshake and send data\nssl_sock.sendall(b'Hello, server!')\ndata = ssl_sock.recv(1024)\n\n# Close the SSL/TLS connection\nssl_sock.close()\n```\n",
"bugtrack_url": null,
"license": "",
"summary": "This is a module to interact with Server and Client",
"version": "0.0.2",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e551830474e88117d80cec3958fdaf229567f0633df7cceaa5e9ad43c9354312",
"md5": "c53e54ae8b54ec95a9be3eb78be7b16b",
"sha256": "1b1d97bea13c63692f3958b507f7e4fb2fbb015627e5478d979846a587c861cd"
},
"downloads": -1,
"filename": "JohnServerAPI-0.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c53e54ae8b54ec95a9be3eb78be7b16b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 8983,
"upload_time": "2023-03-20T01:37:59",
"upload_time_iso_8601": "2023-03-20T01:37:59.294555Z",
"url": "https://files.pythonhosted.org/packages/e5/51/830474e88117d80cec3958fdaf229567f0633df7cceaa5e9ad43c9354312/JohnServerAPI-0.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "39f43a4b1edf84aa27427f755e26ad0092cac486706eed614a89b40807298d15",
"md5": "3c37e5e6068479c74e1e3203d1a0983b",
"sha256": "b545be39f598ac4b9d1ac66225349e7f673661ad5e84332859940d4015edfb4d"
},
"downloads": -1,
"filename": "JohnServerAPI-0.0.2.tar.gz",
"has_sig": false,
"md5_digest": "3c37e5e6068479c74e1e3203d1a0983b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7911,
"upload_time": "2023-03-20T01:38:02",
"upload_time_iso_8601": "2023-03-20T01:38:02.289431Z",
"url": "https://files.pythonhosted.org/packages/39/f4/3a4b1edf84aa27427f755e26ad0092cac486706eed614a89b40807298d15/JohnServerAPI-0.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-20 01:38:02",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "johnserverapi"
}