This tutorial is for advanced users and a minimum knowledge about post/get methods as well as networking is recommended
You will require:
- Python installed
- Flask library installed
- Knowledge about networking, Lua/Python/Html
- Port forwarding
Today I will show you a simple code that I wrote about how easy you can link a Roblox server to a Python code running on a separate computer.
First things first, lets start with the basic POST/GET principle:
HTTP GET/POST DIAGRAM
The Server will be the host computer which runs the python code, and the Client is our Roblox game.
To make it work, we need to host a website which will receive our requests and pass them to the python code.
For that, I’m gonna be using Flask library, as it’s easy to use and provide a lot of support.
To install it, use the following command:
pip install flask in the terminal/command prompt.
Next, we need to make the HTML interface for our website the client is gonna connect to.
Here’s a code that I used (it needs to be named “index.html” and placed in a folder named ‘templates’, next to our python script):
<html> <title> My http test page </title> <body> <p> Here we have the methods: <form method="POST" action="/test"></form> <form method="GET" action="/test"></form> </p> </body> </html>
This code provides a connection between site forms(post/get) and code.
Next, for the back end we have our flask code:
from flask import Flask, render_template, request app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/test', methods=['POST', 'GET']) def test(): if request.method == 'GET': msg = "this is a response to roblox instance, sent from python" print("SENT >>", msg) return msg print("RECEIVED >>",list(request.form)) return render_template('index.html') app.run(host='0.0.0.0',port='1111',debug=False)
Our site is going to be hosted on the local machine(0.0.0.0) on the port 1111.
For testing purposes, you can easily access the website by typing in the browser
Remember! This will only work as long as your script is running!
As you run the script and access your brand new website(which exists only in your LAN for now), you should see something like that:
Great! Now for the Roblox part.
For the request processing, we will use HttpService, as shown:
http = game:GetService("HttpService") message = "message sent from roblox" print("SENT >> "..message) http:PostAsync("http://localhost:1111/test", message,Enum.HttpContentType.ApplicationUrlEncoded) print("RECEIVED >> "..http:GetAsync("http://localhost:1111/test"))
If everything is set up correctly, while having the code running on the same machine we have the Roblox Studio open, we will see the following:
Awesome! You made a great job putting them together!
But how do I send data ?
Well, if you need to send some data to your computer, you will need to use the POST method, and this line of code in your roblox script:
http:PostAsync("http://localhost:1111/test", DATA_HERE, Enum.HttpContentType.ApplicationUrlEncoded)
and receive it in the Flask code by doing this
which will return a list of sent data
If you want to retrieve data from computer, use the GET method, as shown:
which will return data you asked for.
Warning! The following part is not recommended to implement due to the fact you are exposing your network to the wide internet! Continue on your own risk!
Now, for the hard part, to make it work in an actual game, we need to change a few things.
To make the Roblox servers be able to access our Flask website, we need to establish a port forwarding feature on you router/modem to be able to access the website from outside your house.
For that, you need to log into your router/modem and set a new Virtual Servers entry with the service port of 1111, IP address to your computer/next router leading to your computer which you need to set up as well. Protocol set to All (UDP and TCP), Status on Enabled aaand Done!
now you should be able to access the website from outside your local network. For the roblox part, will need to change from ‘localhost’ to your public IP address (which is extremely dangerous), or buy a domain to hide your IP and use it instead of showing your public IP address.
Some routers may encounter issues forwarding your requests. For that you can tweak DMZ(demilitarized zone) settings as well.
Again, exposing your IP is not recommended, so for this tutorial I used it only on localhost.
I hope this tutorial helped you in understanding POST/GET requests and further game creating.