Last active
May 6, 2023 18:00
-
-
Save blockinhead/fb5d4570b41d9fbd96bc09aec22438ca to your computer and use it in GitHub Desktop.
a simple http to maya gateway on flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| from flask import Flask, request, jsonify | |
| MAYA_HOST = '127.0.0.1' | |
| MAYA_MEL_PORT = 7003 | |
| HTML = f''' | |
| <!doctype html> | |
| <title>htom</title> | |
| <h1>your maya command</h1> | |
| <form method=post> | |
| <input type=text name=command><br/> | |
| <input type=submit> | |
| </form> | |
| ''' | |
| app = Flask(__name__) | |
| @app.route('/', methods=['GET', 'POST']) | |
| def send_command(): | |
| if request.method == 'GET': | |
| return HTML | |
| command = request.form['command'] | |
| print(f'sending {command}') | |
| client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| client.connect((MAYA_HOST, MAYA_MEL_PORT)) | |
| v = client.send(command.encode('utf8')) | |
| print(f'sent {type(v)} {v}') | |
| v = client.recv(1024) | |
| print(f'received {type(v)} {v}') | |
| client.close() | |
| response = jsonify({'result': str(v)}) | |
| response.headers['Content-Type'] = 'application/json' | |
| response.headers['Access-Control-Allow-Origin'] = '*' | |
| return response | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=5001, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment