Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # get_data_from_localhost.py
- # kept hearing a whole lot of people swear this the builtin attempt is an impossible feat, even warned by ChatGPT, but... it seems to work perfectly fine
- import http.server
- import socketserver
- import webbrowser
- import json
- import urllib.parse
- html_content = """
- <html>
- <head>
- <script>
- function multiplyNumbers() {
- var numbers = [1, 2, 3, 4, 5];
- var multipliedNumbers = [];
- for (var i = 0; i < numbers.length; i++) {
- multipliedNumbers.push(numbers[i] * 9);
- }
- var data = JSON.stringify(multipliedNumbers);
- var url = 'http://localhost:8080?data=' + encodeURIComponent(data);
- var xhr = new XMLHttpRequest();
- xhr.open('GET', url, true);
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4 && xhr.status === 200) {
- document.write(xhr.responseText);
- }
- };
- xhr.send();
- return multipliedNumbers;
- }
- document.write(multiplyNumbers());
- </script>
- </head>
- <body></body>
- </html>
- """
- def http_server():
- class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
- def do_GET(self):
- if self.path == '/':
- self.send_response(200)
- self.send_header("Content-type", "text/html")
- self.end_headers()
- self.wfile.write(html_content.encode())
- query_components = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query)
- if 'data' in query_components:
- data = query_components['data'][0]
- print(data)
- with socketserver.TCPServer(('localhost', 8080), MyHttpRequestHandler) as httpd:
- webbrowser.open('http://localhost:8080')
- httpd.serve_forever()
- http_server()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement