I have an application built on Django. Initially, I constructed a few APIs using the Django REST API framework, which worked fine. Later, I implemented web sockets to listen to a server.
I started the application server on port 5010 and the socket on which this application listens to the server is 8010.
However, after implementing web sockets, my API calls started to fail(screenshot below).
Observation: I noticed that the API call doesn't fail after POST operation. The failure is at the point where I access the API at http://127.0.0.1:5010/subscribe/. That is, when I go to http://127.0.0.1:5010/subscribe/ in my browser, that is when I see the failure as shown in the above picture.
If I access the API http://127.0.0.1:5010/subscribe/ by deleting the code for web sockets, the API call works fine (screenshot below)
Please explain if using both REST API framework and web sockets would be possible in Django.
I guess you are using the wrong HTTP request in your case.
In Response header, the allowed HTTP request are POST and OPTIONS. But you are attempting to make GET request through web browser.
Try making a POST request instead.
Related
I'm in the process of connecting my React Native UI to Python Django backend using REST framework and unsure of how to go about fetching the data from backend.
I used the fetch(URL) as you can see in the SS below:
The error I get:
I also added my phone as an adb device and connected it through a USB cable before running the app, but same issue. Any suggestions as to how to go about React Native UI and Python Django REST API integration?
A bit late but I hope it will help the next ones.
You cannot use a regular url when connecting DRF (or any other API framework) to a react native app so you need to tunnel it through a reverse proxy such as ngrok. Short answer is, download ngrok, put it in your environment variable then in your shell type :
ngrok http 8000
You will receive an https to replace your localhost:8000 in your API url. Good thing to do is to register to ngrok not to have to replace this new url too often, I think it is only stable for an hour or two if not registered.
Last but not least, in your CORS config whitelist (CORS_ALLOWED_ORIGINS), don't forget to put this new url or you'll get a 404.
Good luck
I’ve got a standard client-server set-up with ReScript (ReasonML) on the front-end and a Python server on the back-end.
The user is running a separate process on localhost:2000 that I’m connecting to from the browser (UI). I can send requests to their server and receive responses.
Now I need to issue those requests from my back-end server, but cannot do so directly. I’m assuming I need some way of doing it through the browser, which can talk to localhost on the user’s computer.
What are some conceptual ways to implement this (ideally with GraphQL)? Do I need to have a subscription or web sockets or something else?
Are there any specific libraries you can recommend for this (perhaps as examples from other programming languages)?
I think the easiest solution with GraphQL would be to use Subscriptions indeed, the most common Rescript GraphQL clients already have such a feature, at least ReasonRelay, Reason Apollo Hooks and Reason-URQL have it.
I've developed a Python Flask Back-end app which allows me to do some HTTP requests on a Jsonfile (a sort of database) such as GET (to see a list of items) or POST (to create a new item in the Json database). Until now, I used Postman to test my app and all worked well. However, I'd like to develop a Python Flask Front-end app in order to create a graphical interface (in a web browser with jinja templates) to do the same requests. The problem is, I don't know how to begin my new app, I've googled all my questions but found no answer...
How can we "link" front-end and back-end apps in order to get the information from the web brower, make the requests via the back-end and then send the response with the front-end?
Using RESTful API.
A infrastructure solution could be (a classic one):
Your app server listening on 5000. It uses the REST architectural.
Your frontend server listening on 80/443. It makes requests to your app server to get the data and fill your html template with this data. The requests can be made with a library like requests. The requests library is one of the simpliest but you can choose another one to make HTTP requests.
Workflow:
Client <-HTTP requests-> Frontend <-HTTP requests made with requests->
App Server <--> Database
Advantage:
One of the advantage of this architecture: you can easily duplicate your servers or having multiple app servers responsible of different tasks. They can be running on the same machine or separated machines.
Serving static files:
If you are talking about serving static files for the frontend, then you should use an existing Webserver like Nginx (html/css/js files).
I'm trying to build a webserver using Apache and I want to establish two-way of communication between my python application and webserver where I can listen for requests from apache and return data back, in the same time I want the client to interact only with the webserver.
In my python program I'm using python python-twisted and pymodbus (MODBUS-TCPIP) to get some data from a PLC.
I have already configured a websocket in python using
from autobahn.twisted.websocket import WebSocketServerProtocol, \
WebSocketServerFactory
I'm thinking to configure a intermediate web which will be initiated upon http request of the main website and open websocket connection with python and start receiving data, where the clients (browsers) can still request data using jquery.
I don't know if this is the right way to go. Do you guys have better way to pass data variable between python and apache back and forth?
If I understood correctly, you want to make PLC data accessible on a web page.
The right way to develop a web application in Python is to use a web framework like Django, or (if you want something lighter) bottle
With this, you can develop a web page where you connect to your PLC over Modbus, query some values, disconnect and return a response with data as json.
But I think, this is not the best architecture because if you have several people accessing your web page, it will cause several connections to your PLC and you may have poor performance for your web app.
I think you had better develop a modbus program which is reading the data from the PLC and store it into a database. Then your web app just gets the data from the database.
I hope it helps
Newbie working on a Salesforce project for a job interview. I'm trying to build a Django form that will submit a case ticket to the Salesforce backend. Using django-salesforce django-salesforcelibrary, and I'm at the point where I'm creating the a new connected app. How do I deal with the callback URL if I'm testing on localhost? The callback url needs to be an https secure connection. Can I just set the callback as http://localhost:8000? Having a hard time figuring it out and on a strict time limit so no time to learn the salesforce API.
No you cannot do that. Because they will try to redirect you to that site also few oauth providers need it a valid url to do so.
You can use https://ngrok.com to publicly expose your local server.
ngrok exposes your local server and port to a publicly accessible url. Internally it created a tunnel from the generated url and your machine. So you can expose your local django server to world using it.
Then pass on the generated link to the OAuth app.