Commit d6423657 authored by Jiabo Li's avatar Jiabo Li
Browse files

Added cm_client.html and http_forwarder.py

parent 2b1cfcf3
Loading
Loading
Loading
Loading

cm_client.html

0 → 100644
+60 −0
Original line number Diff line number Diff line
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Form</title>
    <style>
        /* Add some basic styling */
        body {
            font-family: Arial, sans-serif;
            margin: 40px;
        }
        form {
            display: flex;
            flex-direction: column;
            width: 300px;
        }
        textarea {
            resize: none;
            height: 100px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <form id="message-form">
        <label for="message">Your message to ChatGPT or PyMOL command directly to PyMOL:</label>
        <textarea id="message" name="message">chat fetch 1hiv and color red for chain A</textarea>
        <button type="submit">Send Message</button>
    </form>

    <script>
        // JavaScript code to handle form submission
        document.getElementById('message-form').addEventListener('submit', async (event) => {
            event.preventDefault(); // Prevent the form from submitting and refreshing the page

            const message = document.getElementById('message').value;
            const remoteServiceUrl = 'http://localhost:8000/send_message';

            try {
                const response = await fetch(remoteServiceUrl, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'text/plain'
                    },
                    body: message
                });

                if (response.ok) {
                    alert('Message sent successfully!');
                } else {
                    alert('Error: ' + response.statusText);
                }
            } catch (error) {
                alert('Error: ' + error.message);
            }
        });
    </script>
</body>
</html>

http_forwarder.py

0 → 100644
+22 −0
Original line number Diff line number Diff line
from flask import Flask, request
import socket

app = Flask(__name__)

@app.route('/send_message', methods=['POST'])
def send_message():
    message = request.data.decode('utf-8')
    remote_service_host = 'localhost'
    remote_service_port = 8100

    try:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
            client_socket.connect((remote_service_host, remote_service_port))
            client_socket.sendall(message.encode('utf-8'))
            return 'Message sent successfully', 200
    except Exception as e:
        return f'Error: {e}', 500

if __name__ == '__main__':
    app.run(port=8000)