* [edk2-devel] [edk2-libc Patch 0/1] AppPkg/Python - Sample scripts to exercise socket functionality @ 2023-10-27 16:27 Jayaprakash, N 2023-10-27 16:27 ` [edk2-devel] [edk2-libc Patch 1/1] ek2-libc: Sample python scripts for socket client capabilities on UEFI shell Jayaprakash, N [not found] ` <179203AC4BBFEE91.3183@groups.io> 0 siblings, 2 replies; 4+ messages in thread From: Jayaprakash, N @ 2023-10-27 16:27 UTC (permalink / raw) To: devel; +Cc: Jayaprakash N This commit provides 2 sample scripts namely http_echo_client.py and http_echo_server.py to exercise the socket capabilities on UEFI shell using the http library from Python UEFI interpreter. Jayaprakash N (1): ek2-libc: Sample python scripts for socket client capabilities on UEFI shell .../PyMod-3.6.8/Lib/http_echo_client.py | 81 +++++++++++++++++++ .../PyMod-3.6.8/Lib/http_echo_server.py | 61 ++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py -- 2.40.0.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110221): https://edk2.groups.io/g/devel/message/110221 Mute This Topic: https://groups.io/mt/102224954/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=- ^ permalink raw reply [flat|nested] 4+ messages in thread
* [edk2-devel] [edk2-libc Patch 1/1] ek2-libc: Sample python scripts for socket client capabilities on UEFI shell 2023-10-27 16:27 [edk2-devel] [edk2-libc Patch 0/1] AppPkg/Python - Sample scripts to exercise socket functionality Jayaprakash, N @ 2023-10-27 16:27 ` Jayaprakash, N 2023-10-28 10:25 ` Laszlo Ersek [not found] ` <179203AC4BBFEE91.3183@groups.io> 1 sibling, 1 reply; 4+ messages in thread From: Jayaprakash, N @ 2023-10-27 16:27 UTC (permalink / raw) To: devel; +Cc: Jayaprakash N, Rebecca Cran, Michael D Kinney REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4580 This BZ has been created to provide the sample python scripts to demonstrate the socket client capabilities using http library on UEFI shell with the help of Python UEFI interpreter. The http_echo_client.py and http_echo_server.py scripts are provided as sample scripts to exercise the python http library from UEFI shell. Cc: Rebecca Cran <rebecca@bsdio.com> Cc: Michael D Kinney <michael.d.kinney@intel.com> Cc: Jayaprakash N <n.jayaprakash@intel.com> Signed-off-by: Jayaprakash Nevara <n.jayaprakash@intel.com> --- .../PyMod-3.6.8/Lib/http_echo_client.py | 81 +++++++++++++++++++ .../PyMod-3.6.8/Lib/http_echo_server.py | 61 ++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py new file mode 100644 index 0000000..ea0368d --- /dev/null +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py @@ -0,0 +1,81 @@ +""" +This is a sample HTTP echo client sends data to the server +and gets echoed data from the server in response body +and prints the same to the console. + +Note: This application needs to be run from UEFI shell using +the Python UEFI interpreter. +""" + +import sys +import time +from http import client +from http.client import HTTPException +import traceback + +_max_retries = 10 +_retry_count = 0 + + +def _print_usage(): + print("Sample http echo client application") + print("Usage:") + print("python.efi http_echo_client.py <ServerIPAddress>") + + +if len(sys.argv) != 2: + _print_usage() + sys.exit(0) + +if sys.argv[1] == "-h": + _print_usage() + sys.exit(0) + +http_server = sys.argv[1] +while True: + try: + name = input("Enter the parameter name:") + value = input("Enter parameter value:") + print("Connecting to server to send a get request with following parameter") + print("{}={}".format(name, value)) + # replace space with %20 + value = value.replace(" ", "%20") + conn = client.HTTPConnection(http_server) + # Send GET request with some data + conn.request("GET", "/echo?{}={}".format(name, value)) + rsp = conn.getresponse() + if rsp.status == 204: + print("No content") + break + elif rsp.status == 200: + data_received = rsp.read() + # replace %20 with space character before displaying to console + data_received = data_received.replace(b"%20", b" ") + print("from server:{}".format(data_received)) + conn.close() + print("Closing the connection") + break + else: + print("Invalid response code {}".format(rsp.status)) + conn.close() + print("Closing the connection") + break + except HTTPException as exp: + print("Got exception while connecting to server : {}".format(exp)) + traceback.print_exc() + break + except ConnectionRefusedError as exp: + print("Got exception while connecting to server : {}".format(exp)) + print("Check & start the server, if it is not started") + print( + "Retrying connection after 10 seconds, retry count = {}".format( + _retry_count + 1 + ) + ) + if _retry_count == _max_retries: + print( + "Exceeded max retries {} exiting the application".format(_max_retries) + ) + break + time.sleep(10) + _retry_count += 1 diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py new file mode 100644 index 0000000..eebdf33 --- /dev/null +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py @@ -0,0 +1,61 @@ +""" +This is a sample HTTP echo server that echos the command / data +coming from the client. +Here the data is received from client through GET request in the +form of parameter of GET request. +The parameter is extracted and sent back to the client +in the response body. + +Note that this server sample application needs to be run +on a system booted to OS. +""" + +import os +import socket +import sys +from http.server import BaseHTTPRequestHandler, HTTPServer +from http.client import parse_headers + + +class MyHTTPRequestHandler(BaseHTTPRequestHandler): + """HTTP request handler class""" + + # Handle GET command + def do_GET(self): + print("path {}".format(self.path)) + path = self.path.split("?")[0] + param_name = self.path.split("?")[1].split("=")[0] + param_value = self.path.split("?")[1].split("=")[1] + print("param name {} value = {}".format(param_name, param_value)) + if path == "/echo": + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write( + bytes("{}={}".format(param_name, param_value), encoding="utf-8") + ) + else: + print("invalid request") + self.send_response(204) + + +def run(): + try: + print("Starting the server...") + server_address = (socket.gethostbyname(socket.gethostname()), 80) + print("Server address :", server_address) + httpd = HTTPServer(server_address, MyHTTPRequestHandler) + print("\n\n\nPress CTRL+C to exit server application") + httpd.serve_forever() + except KeyboardInterrupt as exp: + print("KeyboardInterrupt") + sys.exit(0) + except Exception as exp: + print(str(exp)) + + +if __name__ == "__main__": + if os.name == "edk2": + print("HTTP echo server not supported on EDk2") + sys.exit(0) + run() -- 2.40.0.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110222): https://edk2.groups.io/g/devel/message/110222 Mute This Topic: https://groups.io/mt/102224955/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=- ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [edk2-devel] [edk2-libc Patch 1/1] ek2-libc: Sample python scripts for socket client capabilities on UEFI shell 2023-10-27 16:27 ` [edk2-devel] [edk2-libc Patch 1/1] ek2-libc: Sample python scripts for socket client capabilities on UEFI shell Jayaprakash, N @ 2023-10-28 10:25 ` Laszlo Ersek 0 siblings, 0 replies; 4+ messages in thread From: Laszlo Ersek @ 2023-10-28 10:25 UTC (permalink / raw) To: devel, n.jayaprakash; +Cc: Rebecca Cran, Michael D Kinney On 10/27/23 18:27, Jayaprakash, N wrote: > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4580 > > This BZ has been created to provide the sample python scripts > to demonstrate the socket client capabilities using http library > on UEFI shell with the help of Python UEFI interpreter. > The http_echo_client.py and http_echo_server.py scripts > are provided as sample scripts to exercise the python http library > from UEFI shell. > > Cc: Rebecca Cran <rebecca@bsdio.com> > Cc: Michael D Kinney <michael.d.kinney@intel.com> > Cc: Jayaprakash N <n.jayaprakash@intel.com> > Signed-off-by: Jayaprakash Nevara <n.jayaprakash@intel.com> > --- > .../PyMod-3.6.8/Lib/http_echo_client.py | 81 +++++++++++++++++++ > .../PyMod-3.6.8/Lib/http_echo_server.py | 61 ++++++++++++++ > 2 files changed, 142 insertions(+) > create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py > create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py > > diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py > new file mode 100644 > index 0000000..ea0368d > --- /dev/null > +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py > @@ -0,0 +1,81 @@ > +""" > +This is a sample HTTP echo client sends data to the server > +and gets echoed data from the server in response body > +and prints the same to the console. > + > +Note: This application needs to be run from UEFI shell using > +the Python UEFI interpreter. > +""" > + > +import sys > +import time > +from http import client > +from http.client import HTTPException > +import traceback > + > +_max_retries = 10 > +_retry_count = 0 > + > + > +def _print_usage(): > + print("Sample http echo client application") > + print("Usage:") > + print("python.efi http_echo_client.py <ServerIPAddress>") > + > + > +if len(sys.argv) != 2: > + _print_usage() > + sys.exit(0) > + > +if sys.argv[1] == "-h": > + _print_usage() > + sys.exit(0) > + > +http_server = sys.argv[1] > +while True: > + try: > + name = input("Enter the parameter name:") > + value = input("Enter parameter value:") > + print("Connecting to server to send a get request with following parameter") > + print("{}={}".format(name, value)) > + # replace space with %20 Better use <https://docs.python.org/3/library/urllib.parse.html#url-quoting> here, I'd think. Laszlo > + value = value.replace(" ", "%20") > + conn = client.HTTPConnection(http_server) > + # Send GET request with some data > + conn.request("GET", "/echo?{}={}".format(name, value)) > + rsp = conn.getresponse() > + if rsp.status == 204: > + print("No content") > + break > + elif rsp.status == 200: > + data_received = rsp.read() > + # replace %20 with space character before displaying to console > + data_received = data_received.replace(b"%20", b" ") > + print("from server:{}".format(data_received)) > + conn.close() > + print("Closing the connection") > + break > + else: > + print("Invalid response code {}".format(rsp.status)) > + conn.close() > + print("Closing the connection") > + break > + except HTTPException as exp: > + print("Got exception while connecting to server : {}".format(exp)) > + traceback.print_exc() > + break > + except ConnectionRefusedError as exp: > + print("Got exception while connecting to server : {}".format(exp)) > + print("Check & start the server, if it is not started") > + print( > + "Retrying connection after 10 seconds, retry count = {}".format( > + _retry_count + 1 > + ) > + ) > + if _retry_count == _max_retries: > + print( > + "Exceeded max retries {} exiting the application".format(_max_retries) > + ) > + break > + time.sleep(10) > + _retry_count += 1 > diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py > new file mode 100644 > index 0000000..eebdf33 > --- /dev/null > +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py > @@ -0,0 +1,61 @@ > +""" > +This is a sample HTTP echo server that echos the command / data > +coming from the client. > +Here the data is received from client through GET request in the > +form of parameter of GET request. > +The parameter is extracted and sent back to the client > +in the response body. > + > +Note that this server sample application needs to be run > +on a system booted to OS. > +""" > + > +import os > +import socket > +import sys > +from http.server import BaseHTTPRequestHandler, HTTPServer > +from http.client import parse_headers > + > + > +class MyHTTPRequestHandler(BaseHTTPRequestHandler): > + """HTTP request handler class""" > + > + # Handle GET command > + def do_GET(self): > + print("path {}".format(self.path)) > + path = self.path.split("?")[0] > + param_name = self.path.split("?")[1].split("=")[0] > + param_value = self.path.split("?")[1].split("=")[1] > + print("param name {} value = {}".format(param_name, param_value)) > + if path == "/echo": > + self.send_response(200) > + self.send_header("Content-type", "text/plain") > + self.end_headers() > + self.wfile.write( > + bytes("{}={}".format(param_name, param_value), encoding="utf-8") > + ) > + else: > + print("invalid request") > + self.send_response(204) > + > + > +def run(): > + try: > + print("Starting the server...") > + server_address = (socket.gethostbyname(socket.gethostname()), 80) > + print("Server address :", server_address) > + httpd = HTTPServer(server_address, MyHTTPRequestHandler) > + print("\n\n\nPress CTRL+C to exit server application") > + httpd.serve_forever() > + except KeyboardInterrupt as exp: > + print("KeyboardInterrupt") > + sys.exit(0) > + except Exception as exp: > + print(str(exp)) > + > + > +if __name__ == "__main__": > + if os.name == "edk2": > + print("HTTP echo server not supported on EDk2") > + sys.exit(0) > + run() -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110239): https://edk2.groups.io/g/devel/message/110239 Mute This Topic: https://groups.io/mt/102224955/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/1913456212/xyzzy [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=- ^ permalink raw reply [flat|nested] 4+ messages in thread
[parent not found: <179203AC4BBFEE91.3183@groups.io>]
* Re: [edk2-devel] [edk2-libc Patch 1/1] ek2-libc: Sample python scripts for socket client capabilities on UEFI shell [not found] ` <179203AC4BBFEE91.3183@groups.io> @ 2023-11-10 15:41 ` Jayaprakash, N 0 siblings, 0 replies; 4+ messages in thread From: Jayaprakash, N @ 2023-11-10 15:41 UTC (permalink / raw) To: devel@edk2.groups.io, Jayaprakash, N; +Cc: Rebecca Cran, Kinney, Michael D These are simple Python apps to demonstrate the usage of socket communication using the HTTP library. Reviewed-by : Jayaprakash N <n.jayaprakash@intel.com> Regards, JP -----Original Message----- From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Jayaprakash, N Sent: Friday, October 27, 2023 9:57 PM To: devel@edk2.groups.io Cc: Jayaprakash, N <n.jayaprakash@intel.com>; Rebecca Cran <rebecca@bsdio.com>; Kinney, Michael D <michael.d.kinney@intel.com> Subject: [edk2-devel] [edk2-libc Patch 1/1] ek2-libc: Sample python scripts for socket client capabilities on UEFI shell REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4580 This BZ has been created to provide the sample python scripts to demonstrate the socket client capabilities using http library on UEFI shell with the help of Python UEFI interpreter. The http_echo_client.py and http_echo_server.py scripts are provided as sample scripts to exercise the python http library from UEFI shell. Cc: Rebecca Cran <rebecca@bsdio.com> Cc: Michael D Kinney <michael.d.kinney@intel.com> Cc: Jayaprakash N <n.jayaprakash@intel.com> Signed-off-by: Jayaprakash Nevara <n.jayaprakash@intel.com> --- .../PyMod-3.6.8/Lib/http_echo_client.py | 81 +++++++++++++++++++ .../PyMod-3.6.8/Lib/http_echo_server.py | 61 ++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py create mode 100644 AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_client.py new file mode 100644 index 0000000..ea0368d --- /dev/null +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_ +++ client.py @@ -0,0 +1,81 @@ +""" +This is a sample HTTP echo client sends data to the server and gets +echoed data from the server in response body and prints the same to the +console. + +Note: This application needs to be run from UEFI shell using the Python +UEFI interpreter. +""" + +import sys +import time +from http import client +from http.client import HTTPException +import traceback + +_max_retries = 10 +_retry_count = 0 + + +def _print_usage(): + print("Sample http echo client application") + print("Usage:") + print("python.efi http_echo_client.py <ServerIPAddress>") + + +if len(sys.argv) != 2: + _print_usage() + sys.exit(0) + +if sys.argv[1] == "-h": + _print_usage() + sys.exit(0) + +http_server = sys.argv[1] +while True: + try: + name = input("Enter the parameter name:") + value = input("Enter parameter value:") + print("Connecting to server to send a get request with following parameter") + print("{}={}".format(name, value)) + # replace space with %20 + value = value.replace(" ", "%20") + conn = client.HTTPConnection(http_server) + # Send GET request with some data + conn.request("GET", "/echo?{}={}".format(name, value)) + rsp = conn.getresponse() + if rsp.status == 204: + print("No content") + break + elif rsp.status == 200: + data_received = rsp.read() + # replace %20 with space character before displaying to console + data_received = data_received.replace(b"%20", b" ") + print("from server:{}".format(data_received)) + conn.close() + print("Closing the connection") + break + else: + print("Invalid response code {}".format(rsp.status)) + conn.close() + print("Closing the connection") + break + except HTTPException as exp: + print("Got exception while connecting to server : {}".format(exp)) + traceback.print_exc() + break + except ConnectionRefusedError as exp: + print("Got exception while connecting to server : {}".format(exp)) + print("Check & start the server, if it is not started") + print( + "Retrying connection after 10 seconds, retry count = {}".format( + _retry_count + 1 + ) + ) + if _retry_count == _max_retries: + print( + "Exceeded max retries {} exiting the application".format(_max_retries) + ) + break + time.sleep(10) + _retry_count += 1 diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_server.py new file mode 100644 index 0000000..eebdf33 --- /dev/null +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_echo_ +++ server.py @@ -0,0 +1,61 @@ +""" +This is a sample HTTP echo server that echos the command / data coming +from the client. +Here the data is received from client through GET request in the form +of parameter of GET request. +The parameter is extracted and sent back to the client in the response +body. + +Note that this server sample application needs to be run on a system +booted to OS. +""" + +import os +import socket +import sys +from http.server import BaseHTTPRequestHandler, HTTPServer from +http.client import parse_headers + + +class MyHTTPRequestHandler(BaseHTTPRequestHandler): + """HTTP request handler class""" + + # Handle GET command + def do_GET(self): + print("path {}".format(self.path)) + path = self.path.split("?")[0] + param_name = self.path.split("?")[1].split("=")[0] + param_value = self.path.split("?")[1].split("=")[1] + print("param name {} value = {}".format(param_name, param_value)) + if path == "/echo": + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write( + bytes("{}={}".format(param_name, param_value), encoding="utf-8") + ) + else: + print("invalid request") + self.send_response(204) + + +def run(): + try: + print("Starting the server...") + server_address = (socket.gethostbyname(socket.gethostname()), 80) + print("Server address :", server_address) + httpd = HTTPServer(server_address, MyHTTPRequestHandler) + print("\n\n\nPress CTRL+C to exit server application") + httpd.serve_forever() + except KeyboardInterrupt as exp: + print("KeyboardInterrupt") + sys.exit(0) + except Exception as exp: + print(str(exp)) + + +if __name__ == "__main__": + if os.name == "edk2": + print("HTTP echo server not supported on EDk2") + sys.exit(0) + run() -- 2.40.0.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#111039): https://edk2.groups.io/g/devel/message/111039 Mute This Topic: https://groups.io/mt/102508927/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=- ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-11-10 15:41 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2023-10-27 16:27 [edk2-devel] [edk2-libc Patch 0/1] AppPkg/Python - Sample scripts to exercise socket functionality Jayaprakash, N 2023-10-27 16:27 ` [edk2-devel] [edk2-libc Patch 1/1] ek2-libc: Sample python scripts for socket client capabilities on UEFI shell Jayaprakash, N 2023-10-28 10:25 ` Laszlo Ersek [not found] ` <179203AC4BBFEE91.3183@groups.io> 2023-11-10 15:41 ` Jayaprakash, N
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox