I've recently started learning Python personally, so I wondered if I could use Salesforce from Python.
The following was used to create this sample
To supplement a little
Bottle
A framework for creating web applications in Python. Other frameworks for creating web applications in Python include Django. Bottle is a lightweight framework among them, and it is said that the minimum necessary items are available, so I chose it.
virtualenv
A library that creates a virtual environment for Python. As an image, it may be similar to creating a virtual environment with Virtualbox and Vagrant.
simple-salesforce
A module for operating Salesforce from Python. Looking at the official documentation, it seems like you're hitting the REST API.
There is only a minimum of implementation
index.tpl
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Bottle Index template</title>
</head>
<body>
<h1>{{msg}}</h1>
<h2></h2>
<form action="" method="post">
<input type="text" name="username" placeholder="Username" />
<input type="password" name="password" placeholder="Password" />
<input type="password" name="security_token" placeholder="SecurityToken" />
<input type="submit" value="Login" />
</form>
</body>
</html>
index.py
from bottle import route, run, template, request
from simple_salesforce import Salesforce
@route('/')
def index(msg='This page is home page.'):
return template('index', msg=msg)
@route('/', method=["POST"])
def login_salesforce():
uname = request.POST.getunicode("username")
upw = request.POST.getunicode("password")
token = request.POST.getunicode("security_token")
try:
sf = Salesforce(username=uname, password=upw, security_token=token)
return template("login_success")
except Exception as e:
return template("login_error")
finally:
print("Login process completed")
run(host='localhost', port=8080, debug=True, reloader=True)
It's really just the bare minimum. It will be added as needed in the future.
The source at this stage is on Github's BottleSalesforceEnv.
Recommended Posts