Integrate Python with MuleSoft

Python 🐍

Python is a programming language that is commonly used to create websites and applications, automate operations, and perform data analysis. Python is a general-purpose programming language, which means it can be used to develop a wide range of applications and isn’t specialized for any particular problem. Because of its versatility and beginner-friendliness, it has become one of the most widely used programming languages today.

Why is Python so popular?

Although, Python is known for being slow 🐢 yet it’s very popular. Here are some reasons:

  • Easy to learn and use
    Python is a High-Level Programming Language that is close to writing pseudocode in English and hence people find it a bit easy
  • Libraries
    “Don’t reinvent the wheel” and so there are hundreds of useful libraries which get job done faster
  • Community Support
    It has rich community support hosting meetups and events to clear fellow developers’ doubts.

These are just a few of many other reasons why python is trending.

Here’s a StackOverflow survey showing popular ones :
Click here for the survey insights.

Integrating python with MuleSoft Scripting Module
  • Create a new app
  • Add Scripting module as below
  • Add Jython dependency

<dependency>
 <groupId>org.python</groupId>
 <artifactId>jython-standalone</artifactId>
 <version>2.7.2</version>
</dependency>

  • Also include jython shared library

<configuration>
<sharedLibraries>
<sharedLibrary>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
</sharedLibrary>
</sharedLibraries>
</configuration>

  • Now we should be able to add Scripting Execute in our flow to look like something as below
  • Here’s the python code that uses request library to call a website and return the status code
import requestsdef request_website(site):	
    r = requests.get(site, verify=False)
    return r.status_code
    result = request_website(site)
  • Alternatively, we can also add a reference to the script file using ${file::app.py} considering app.py is your script file present in src/resources
  • Files should look like below:
  • Finally, we add a Transform Message to display the content to the calling client
  • Now let’s see it in action using Postman

There we have a working MuleSoft application integrated with python.

Hope you liked it. Cheers 🍻

Tirthankar Kundu

Tirthankar Kundu