From nobody Tue May 14 17:43:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110222+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110222+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1698424048; cv=none; d=zohomail.com; s=zohoarc; b=JnZjXxKikr558PUsHMKW9thcuaTJ8ndP1nHle1fkFp+yiXNjutunU5vGF9rMf363z0nP3gLMy00jcY9iZW3dvodwau/dLOo/bxsx/6ef38sdod2BcnrLtn0gmnER2UMU0ldy2yGN+CVPU8EYj5BdJ1qAr1uRhTM/n2SFOycZ8jM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1698424048; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=QFWqI2JF5PUW9XXq70NBkWl9cwy1DaXf04kguvPiFaE=; b=Wvw6TRPIQkoVIwh4hjEMHVJXwqAf4ZrS1BgwzcwRTxDkhQ0Fg0wpB/3/JZd1XE8uo0HkC8gXViy3lE8LucBPSlpI/CncWEDzGXMxnK3JeL8xx/cqJ2OoSX2237nQpumOwmtznxWEUnP4e0xcD/bmRGvd8z+jsaO6a7/xnugC+eE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110222+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1698424048834242.59866421347692; Fri, 27 Oct 2023 09:27:28 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=5nwjTEdmvAr1QgwFq2aginf84vZoDl+RBjSfVcr0iBA=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1698424048; v=1; b=OhdhddaDtJ0wuH9eY9QEhb2FHPF3KGWZmPkEcaLRRJsHda8peBYJMvj789ga0Vy9tT3XaCpN BYBk9beLE6wgkYQfQg2zhdd2EMetlkRmAGpzH+lqtTLg7vhIQyAV1/zPHCW4JGJw/hfUgTZl/al LYLnGYK6jlkpyFXxYvhblMyI= X-Received: by 127.0.0.2 with SMTP id rRzRYY1788612xhi3eqrY4ie; Fri, 27 Oct 2023 09:27:28 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.126]) by mx.groups.io with SMTP id smtpd.web10.11385.1698424046415295725 for ; Fri, 27 Oct 2023 09:27:27 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10876"; a="372857766" X-IronPort-AV: E=Sophos;i="6.03,256,1694761200"; d="scan'208";a="372857766" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Oct 2023 09:27:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10876"; a="825386295" X-IronPort-AV: E=Sophos;i="6.03,256,1694761200"; d="scan'208";a="825386295" X-Received: from rboorax-mobl1.gar.corp.intel.com (HELO njayapra-mobl.gar.corp.intel.com) ([10.213.67.125]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Oct 2023 09:27:25 -0700 From: "Jayaprakash, N" To: devel@edk2.groups.io Cc: Jayaprakash N , Rebecca Cran , Michael D Kinney Subject: [edk2-devel] [edk2-libc Patch 1/1] ek2-libc: Sample python scripts for socket client capabilities on UEFI shell Date: Fri, 27 Oct 2023 21:57:18 +0530 Message-Id: <20231027162718.830-2-n.jayaprakash@intel.com> In-Reply-To: <20231027162718.830-1-n.jayaprakash@intel.com> References: <20231027162718.830-1-n.jayaprakash@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,n.jayaprakash@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: oRfDrcUANnYmiMRuJGA3l58Ox1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1698424049476100002 Content-Type: text/plain; charset="utf-8" REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4580 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 Cc: Michael D Kinney Cc: Jayaprakash N Signed-off-by: Jayaprakash Nevara --- .../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_e= cho_client.py b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/htt= p_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_cli= ent.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=20 +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 =3D 10 +_retry_count =3D 0 + + +def _print_usage(): + print("Sample http echo client application") + print("Usage:") + print("python.efi http_echo_client.py ") + + +if len(sys.argv) !=3D 2: + _print_usage() + sys.exit(0) + +if sys.argv[1] =3D=3D "-h": + _print_usage() + sys.exit(0) + +http_server =3D sys.argv[1] +while True: + try: + name =3D input("Enter the parameter name:") + value =3D input("Enter parameter value:") + print("Connecting to server to send a get request with following p= arameter") + print("{}=3D{}".format(name, value)) + # replace space with %20 + value =3D value.replace(" ", "%20") + conn =3D client.HTTPConnection(http_server) + # Send GET request with some data + conn.request("GET", "/echo?{}=3D{}".format(name, value)) + rsp =3D conn.getresponse() + if rsp.status =3D=3D 204: + print("No content") + break + elif rsp.status =3D=3D 200: + data_received =3D rsp.read() + # replace %20 with space character before displaying to console + data_received =3D 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 =3D {}".for= mat( + _retry_count + 1 + ) + ) + if _retry_count =3D=3D _max_retries: + print( + "Exceeded max retries {} exiting the application".format(_= max_retries) + ) + break + time.sleep(10) + _retry_count +=3D 1 diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/http_e= cho_server.py b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Lib/htt= p_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_ser= ver.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=20 +form of parameter of GET request. +The parameter is extracted and sent back to the client=20 +in the response body. + +Note that this server sample application needs to be run=20 +on a system booted to OS.=20 +""" + +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 =3D self.path.split("?")[0] + param_name =3D self.path.split("?")[1].split("=3D")[0] + param_value =3D self.path.split("?")[1].split("=3D")[1] + print("param name {} value =3D {}".format(param_name, param_value)) + if path =3D=3D "/echo": + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write( + bytes("{}=3D{}".format(param_name, param_value), encoding= =3D"utf-8") + ) + else: + print("invalid request") + self.send_response(204) + + +def run(): + try: + print("Starting the server...") + server_address =3D (socket.gethostbyname(socket.gethostname()), 80) + print("Server address :", server_address) + httpd =3D 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__ =3D=3D "__main__": + if os.name =3D=3D "edk2": + print("HTTP echo server not supported on EDk2") + sys.exit(0) + run() --=20 2.40.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- 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/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-