-
-
Save sovannit/29368d9fbaec238fe7608ff932ac7b2a to your computer and use it in GitHub Desktop.
KiteConnect postback example
This file contains 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
''' | |
Hi traders, | |
Here is a simple generic example for implementing a postback mechanism. You can host this in a remote server or in your local server itself. | |
Run Locally | |
=========== | |
1. Install ngrok from https://ngrok.com/ | |
This is a simple free tool to tunnel your local server and a public domain given by ngrok. Follow the simple tutorial in their website to set it up. | |
2. Run python app and ngrok tunnel | |
Save the following code to "app.py" | |
Run the script by "sudo python app.py" | |
Run ngrok tunnel by "ngrok http 80" | |
3. Update postback url in Kite Connect dashboard | |
In the ngrok command line interface you can see a url in the format "https://<randomstring>.ngrok.io". Copy that url. | |
Go to your app's setup page and paste the url in "Postback URL" field as "https://<randomstring>.ngrok.io/post" | |
Click on save | |
You will have to do this every time you restart ngrok. Because in their free plan the url is not exclusive. | |
4. Test | |
Try some trades from your app. You can see logs in your ngrok interface. | |
Goto "https://<randomstring>.ngrok.io" in your browser. You can see entire log of the day | |
Run in Server | |
=========== | |
1. Setup | |
Save the following code to "app.py" | |
Save your ssl certificates as "cert.pem" and "key.pem" | |
Modify port number to 443 and add ssl_context param to enable https | |
Run the script as "sudo python app.py" | |
2. Update postback url in Kite Connect dashboard | |
Go to your app's setup page and paste the url in "Postback URL" field as "https://<yourdomain>/post" | |
Click on save | |
3. Test | |
Try some trades from your app. | |
Goto "https://<yourdomain>/" in your browser. You can see entire log of the day | |
''' | |
from flask import Flask, request | |
import os | |
import datetime | |
app = Flask(__name__) | |
def log_name(): | |
# logs will be saved in files with current date | |
return datetime.datetime.now().strftime("%Y-%m-%d") + '.txt' | |
@app.route('/post', methods=['POST']) | |
def post(): | |
# post back json data will be inside request.get_data() | |
# as an example here it is being stored to a file | |
f = open(log_name(),'a+') | |
f.write(str(request.get_data())+'\n') | |
f.close() | |
return 'done' | |
@app.route('/') | |
def index(): | |
# show the contents of todays log file | |
if not os.path.exists(log_name()): | |
open(log_name(), 'a+').close() | |
return open(log_name()).read() | |
app.run(debug=True, host='0.0.0.0', port=80) | |
# if you have your own ssl certificates place them in this directory | |
# use this statement to enable https for postbacks | |
# app.run(debug=True, '0.0.0.0', port=443, ssl_context=('cert.pem', 'key.pem')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment