Python HTTP Requests Package
Meets UBot Studio
In this guide you will learn how to use Pythons Requests Package for HTTP in UBot Studio utilizing the Python Plugins “Execute Python” function “$Execute from code”. Additionally, you will learn to use the define command with the Python code provided in the download. Why would you want to do this? Simply put, there are many core and volunteer developers working on the Python language and its libraries so updates are faster, you can also dive into to the source code and fix it yourself. Another reason is security for you and your clients. The Requests package is much older, mature and does many things for you like handle cookies, encoding the url, parses JSON, headers and much more. Finally, it is easy to learn and implement into your bots as you will soon learn below.
Requirements and version used in the tutorial:
- Python 3.7
- Requests 2.22.0 – prp install requests
- Jupyter Notebook – pip install jupyter
- UBot Studio 5.9.55 and any edition that supports the Python plugin
- Python plugin for UBot
Included in the download are .py files, Jupyter Notebook files and .ubot file to run the code. The following images are from the Jupyter Notebook. While the Notebook is not required it is especially helpful as an IDE for building your code for UBot. As you will see below it has cells you can run separately like a UBot node.
As with every Python package/module you need to import your libraries. You can rename them as you like. The standard for requests is req. To run the notebook “cell” press shift + enter.
HTTP Get Request
On the first line we are calling the “get” function with the parameter/argument string of the url we want to visit. The information we get back from the server will be held in the variable resp(response). It is actually an “Object Variable” because you can use the OOP . notation and see you can do many more things with the response.
In line 3 we close() the connection to clean up after ourselves.
If you are not dealing with files you will want to use “text” or “json()”. Otherwise you will use “content” for files and anything else that are bytes.
Finally, we print() the response which happens to be JSON. In the notebook you don’t have to use print() however, in most cases it looks nicer and easier to read. Notice in the output the default “User Agent”. We will fix this next. We wouldn’t want to be blocked immediately now would we.
Set the User Agent String
The requests lib takes some of its keyword args as dictionaries. In this case “headers” highlighted in red above. In the first to lines I set the UA with the desired user agent string. Then I create a dictionary notated by the curly braces. Dictionaries are based on key:value pairs, as you can see I set the value with “UA”. When you run the cell you get a response with your header set to the user agent I put in UA.
Setting a Proxy
The keyword arg for proxites takes a dict as well. In this case we’re setting just the “http” proxy as we can set multiple types if needed ie ftp, https. You can see above how to set a timeout and in the output you can see the result of a bad proxy. More on this later…
Using a Context Manager
Using a context manager is an easy way to close the connection and save a couple of lines. We open the connection as “resp” then use the variable as we wish and the connection closes with the last line.
Creating a Function
Simple function to give you an idea of how to pass the url and call the function. URL was set earlier in the notebook. You can see the url in the output.
Combine the above knowledge
Here I show you how to set more headers including the referrer.
Calling the function
Here I set the proxy to “None” to show how to do it without a proxy. Un-comment and use the proxy to see the error response from the ty/except block. We put it in the try/except block to handle errors and log them, in this case with a print().
HTTP POST Request
A POST is almost the same as a GET except with passing “parameters” in the keyword arg “params”.
This is how you send form data by setting the from inputs into the PRAMS dictionary with the username and password of a form to send to the server. As I briefly mentioned above the library handles the encoding so the server can understand it.
Find Python on Windows 10
If you’ve added Python to your PATH you can type “where python” and get the file path you need for the python plugin. Then goto the “Methods” tab and add it to the function.
Test GET in UBot
Now that we tested our Python code in the notebook we can test it in UBot.
Sweet, it worked!! Now we can put it in a UBot function. But first I wanted you to notice the $Python path function I created make sure you change this to the location of your Python installation on your machine.
GET Custom Define Test
POST Test in UBot