Hey guys I'm having a really strange issue with ngrok and Twilio StudioFlow.
For some reason, the ngrok setup using (type "ngrok http 5000" in command line) and then copy and pasting the link into StudioFlow http widgets has stopped working, when it was working fine earlier. Now, when the widgets are reached in the flow, I get these:
127.0.0.1 - - [11/Feb/2021 02:18:10] "POST /reminders HTTP/1.1" 404 -
127.0.0.1 - - [11/Feb/2021 02:18:10] "POST /week2 HTTP/1.1" 404 -
The flask routes are right (/reminders and /week2) and I am pasting them like so in the widgets (http://8b5dba64ef0c.ngrok.io/reminders) so I am really not sure why this is happening all of a sudden. Why can't twilio find my ngrok tunnel?
For context, I've tried running the flask app on different ports, but that doesn't solve the issue either. That leads me to believe theres something wrong with how to link is being read in to either twilio or my app. I haven't been able to find one though. An example of my code:
#app.route('/reminders', methods = ['POST', 'GET'])
def remmy():
# Start our empty response
resp = MessagingResponse()
if request.method == 'POST':
number = request.form['To']
print(type(number))
part = Part.query.filter_by(phone_num=number).first()
def base_reminder(num):
rem = '[#####]: Don\'t forget to complete your baseline survey to receive an incentive of $35.'
message = client.messages.create(from_='+1###########',
to=num,
body=rem)
resp = MessagingResponse()
msg = resp.message
return str(resp)
if not part.baseline:
base_reminder(part.phone_num)
return str(resp)
Related
from flask import Flask, jsonify, request
app = Flask(__name__)
app.users = {}
app.id_count = 1
#app.route("/ping", methods = ['GET'])
def ping():
return "pong"
#app.route("/sign-up",methods = ['POST'])
def sign_up():
new_user = request.json
new_user["id"] = app.id_count
app.users[app.id_count]= new_user
app.id_count = app.id_count +1
return jsonify(new_user)
if __name__ =='__main__':
app.run()
I tried to send a HTTP request to endpoint /sign-up which is defined as above,
127.0.0.1 - - [22/Nov/2021 22:02:02] "POST /sign-up HTTP/1.1" 404 -
It seems like a really simple problem and I don't see it.
I was running these on WSL2 ubuntu. (both server and client)
and I eventually tried running this on my Windows Pycharm which is not a virtual machine, and it worked fine.
I wonder why /ping works fine tho, will be finishing this course on ordinary windows from now on for sure
I am using python and flask to create a web API. However I am getting trouble in requesting the HTTP status code of my localhost.
My code:
import requests
import flask
app = flask.Flask(__name__)
app.config["DEBUG"] = True
#app.route('/home', methods=['GET'])
def home():
r = requests.get(url="http://localhost:5000/home")
print(r.status_code)
return "Welcome!"
app.run()
Before adding the line for requesting status code, it works fine in my browser (Chrome) and the command prompt show something like this:
127.0.0.1 - - [19/Sep/2019 01:03:54] "GET /home HTTP/1.1" 200 -
After adding the line for requesting, it keeps loading (forever) in my browser and no response in the command prompt.
I have no idea about this problem because it did not show any error and I have read some of the solutions mentioned in other similar problems (like disabling proxy) but it seems not working for me.
Thanks!
Look at it from the point of view of the app. It gets a request for /home and routes it to home(). While servicing that request, the app makes a request for /home, which routes to home(). During the servicing of that request... and so on until some resource is exhausted.
If you intent is to prove that you can make a request to the app from within the app, target a different endpoint.
I'm building a simple web app using Python as part of an online course, currently I'm trying to implement some CRUD operations. This app is a simple restaurant menu server that connects to an SQLite database (using SQLAlchemy) and executes some CRUD operations on it.
Part of the app serves a simple html form to modify a restaurant name (using POST method), when implementing this operation, an error causes the server to return an empty response, and when looking at the log, no POST request was issued and the do_POST body was executed 3 times.
Here's the body of the do_POST method.
def do_POST(self):
try:
if self.path.endswith(....):
(.....)
if self.path.endswith('/edit'):
print('inside post ' + self.path) # debugging message
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
fields = cgi.parse_multipart(self.rfile, pdict)
messageContent = fields.get('newRestaurantName')
r_id = self.path.split('/')[2]
# if I remove .one() from the instruction bellow, the error goes away.
# 'session' is an SQLAlchemy DBSession defined above.
restaurant = session.query(Restaurant).get(r_id).one()
restaurant.name = messageContent[0]
session.add(restaurant)
session.commit()
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.send_header('Location', '/restaurants')
self.end_headers()
return
except:
(....)
And this is the server output when trying to perform the POST method.
Server running at port 8080
10.0.2.2 - - [25/Oct/2018 23:17:27] "GET /restaurants HTTP/1.1" 200 -
10.0.2.2 - - [25/Oct/2018 23:17:30] "GET /restaurants/1/edit HTTP/1.1" 200 -
inside post /restaurants/1/edit
inside post /restaurants/1/edit
inside post /restaurants/1/edit
After removing the .one() from the query expression, the operation executes without issues.
What I'm trying to understand is why I'm having the debugging message printed three times ! whereas I only performed one (unsuccessful) POST request ? Even if there was an exception, shouldn't the do_POST function only execute once ?
(the server is running from inside a Vagrant VM)
Thank you !
I have the following code on my service and when requested the return is always 404.
#app.route('/v1/auth/service', methods=['POST'])
def verifyAuthService():
data = request.get_json()
But in the log file, the service returns 404.
127.0.0.1 - - [TIMEVALUE] "POST /v1/auth/service HTTP/1.1" 404 -
But it works when I use other route. I have checked if the route path or method name are duplicated and didn't find anything.
I request the service method with the following code:
r = requests.post("http://myservice.com:5001/v1/auth/service", json=jPayload)
Maybe was a newbie error, in my init.py file, I haven't imported auth_services.py.
The /v1/auth/service route wasn't interpreted by python so, the route was inaccessible.
Can you try building the URL with below code and match if the route is pointing to exactly same URL which you have called.
from flask import Flask, url_for
app = Flask(__name__)
#app.route('/v1/auth/service', methods=['POST'])
def verifyAuthService():
data = request.get_json()
with app.test_request_context():
print url_for('verifyAuthService')
Hope this helps!
Github offers to send Post-receive hooks to an URL of your choice when there's activity on your repo.
I want to write a small Python command-line/background (i.e. no GUI or webapp) application running on my computer (later on a NAS), which continually listens for those incoming POST requests, and once a POST is received from Github, it processes the JSON information contained within. Processing the json as soon as I have it is no problem.
The POST can come from a small number of IPs given by github; I plan/hope to specify a port on my computer where it should get sent.
The problem is, I don't know enough about web technologies to deal with the vast number of options you find when searching.. do I use Django, Requests, sockets,Flask, microframeworks...? I don't know what most of the terms involved mean, and most sound like they offer too much/are too big to solve my problem - I'm simply overwhelmed and don't know where to start.
Most tutorials about POST/GET I could find seem to be concerned with either sending or directly requesting data from a website, but not with continually listening for it.
I feel the problem is not really a difficult one, and will boil down to a couple of lines, once I know where to go/how to do it. Can anybody offer pointers/tutorials/examples/sample code?
First thing is, web is request-response based. So something will request your link, and you will respond accordingly. Your server application will be continuously listening on a port; that you don't have to worry about.
Here is the similar version in Flask (my micro framework of choice):
from flask import Flask, request
import json
app = Flask(__name__)
#app.route('/',methods=['POST'])
def foo():
data = json.loads(request.data)
print "New commit by: {}".format(data['commits'][0]['author']['name'])
return "OK"
if __name__ == '__main__':
app.run()
Here is a sample run, using the example from github:
Running the server (the above code is saved in sample.py):
burhan#lenux:~$ python sample.py
* Running on http://127.0.0.1:5000/
Here is a request to the server, basically what github will do:
burhan#lenux:~$ http POST http://127.0.0.1:5000 < sample.json
HTTP/1.0 200 OK
Content-Length: 2
Content-Type: text/html; charset=utf-8
Date: Sun, 27 Jan 2013 19:07:56 GMT
Server: Werkzeug/0.8.3 Python/2.7.3
OK # <-- this is the response the client gets
Here is the output at the server:
New commit by: Chris Wanstrath
127.0.0.1 - - [27/Jan/2013 22:07:56] "POST / HTTP/1.1" 200 -
Here's a basic web.py example for receiving data via POST and doing something with it (in this case, just printing it to stdout):
import web
urls = ('/.*', 'hooks')
app = web.application(urls, globals())
class hooks:
def POST(self):
data = web.data()
print
print 'DATA RECEIVED:'
print data
print
return 'OK'
if __name__ == '__main__':
app.run()
I POSTed some data to it using hurl.it (after forwarding 8080 on my router), and saw the following output:
$ python hooks.py
http://0.0.0.0:8080/
DATA RECEIVED:
test=thisisatest&test2=25
50.19.170.198:33407 - - [27/Jan/2013 10:18:37] "HTTP/1.1 POST /hooks" - 200 OK
You should be able to swap out the print statements for your JSON processing.
To specify the port number, call the script with an extra argument:
$ python hooks.py 1234
I would use:
https://github.com/carlos-jenkins/python-github-webhooks
You can configure a web server to use it, or if you just need a process running there without a web server you can launch the integrated server:
python webhooks.py
This will allow you to do everything you said you need. It, nevertheless, requires a bit of setup in your repository and in your hooks.
Late to the party and shameless autopromotion, sorry.
If you are using Flask, here's a very minimal code to listen for webhooks:
from flask import Flask, request, Response
app = Flask(__name__)
#app.route('/webhook', methods=['POST'])
def respond():
print(request.json) # Handle webhook request here
return Response(status=200)
And the same example using Django:
from django.http import HttpResponse
from django.views.decorators.http import require_POST
#require_POST
def example(request):
print(request.json) # Handle webhook request here
return HttpResponse('Hello, world. This is the webhook response.')
If you need more information, here's a great tutorial on how to listen for webhooks with Python.
If you're looking to watch for changes in any repo...
1. If you own the repo that you want to watch
In your repo page, Go to settings
click webhooks, new webhook (top right)
give it your ip/endpoint and setup everything to your liking
use any server to get notified
2. Not your Repo
take the url you want i.e https://github.com/fire17/gd-xo/
add /commits/master.atom to the end such as:
https://github.com/fire17/gd-xo/commits/master.atom
Use any library you want to get that page's content, like:
filter out the keys you want, for example the element
response = requests.get("https://github.com/fire17/gd-xo/commits/master.atom").text
response.split("<updated>")[1].split("</updated>")[0]
'2021-08-06T19:01:53Z'
make a loop that checks this every so often and if this string has changed, then you can initiate a clone/pull request or do whatever you like