I am trying to create a script that acts as a local web-service. I am making an inventory management for my own materials and I would like a python script to host a small server or something where I can type in the IP address, or possibly a command, in my web-browser and it will load the data.
My inventory is stored in a CSV file which, when connected to, the script would parse the csv to an html file and then run a server along with some javascript to make it more responsive.
How would I interface this python module to do all of that? I've never done any direct server programming. I've only done node.js, and then client-side web programming in regards to the internet.
For dealing wit raspberry pi and servers I will suggest Flask, which is Python lightweight web framework. Development in it is very fast. Some reading you can find here (in generally this blog is very good and exhaustive tutorial for Flask):
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux-even-on-the-raspberry-pi
In fact you can run some lighthweight single file solution also with pyramid. look at:
http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/single_file_tasks/single_file_tasks.html
Related
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 cool thing in python that does some simple data manipulation and a bit of machine learning stuff based on user inputs. I'd like to develop an ios app for it, and from what I've read, that app should be in swift as much as possible. I'd like to keep the 'brains' of the app in python on a server so I can develop multiple interfaces to it (a website, possibly an android app as well, a chrome/safari extension).
My app could just be a shortcut to the mobile version of the website, but from what I've read, I can make a better product by writing a custom app for ios.
Can anyone point me to resources describing the most apple-approved way of letting a swift app communicate with a server that hosts a python back end?
welcome to StackOverflow!
iOS does not make a distinction whether the server runs PHP, Python, Java, JS/Node or whatever. All you need is network communication. The easiest way to start would be to simply use HTTPS (it needs to be SSL secured, if you do HTTP requests Apple will reject your app in review).
Apple gives you some tools directly in Swift. All you need should be covered by URLSession: https://developer.apple.com/documentation/foundation/urlsession
There is also a nice tutorial on RayWenderlich: https://www.raywenderlich.com/567-urlsession-tutorial-getting-started
Additional comment:
This of course means you will need to make sure your Python code is available via Network/Internet. A good way to host Python code as a server would be Django (https://www.djangoproject.com/)
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
I'm looking for a bit of web development advice. I'm fairly new to the area but I'm sure there are some gurus out there willing to part with some wisdom.
Objective: I'm interested in controlling a Python application on my computer from my personal web hosted site. I know, this question has been asked several times before but in each case the requirements were a bit different from my own. To reduce the length of this post I'll summarize my objective in a few bullet points:
Personal site is hosted by a web hosting company
Site uses HTML, PHP, MySQL, Python and JavaScript, the majority of everything is coded by me from the ground up
An application that is coded in Python will run on a PC within my home and will communicate with an Arduino board
The app will receive commands from the internet to control actuation via the Arduino, and will transmit sensor data back to the site (such as temperature)
Looking for the communication to be bi-directional, fast and secure
Securing the connection between site and Python app would be most ideal
I'm not looking to connect to the Python application directly, the web server must serve as the 'middle man'
So far I've considered HTTP Post and HTML forms, using sockets (Python app would run as a web server), an IRC bot and reading/writing to a text file stored on the web server.
I was also hoping to have a way to communicate with the Python app without needing to refresh the webpage, perhaps using AJAX or JavaScipt? Maybe with Flash?
Is there something I'm not considering? I feel like I'm missing something. Thanks in advance for the advice!
Just thinking out loud for how I would start out with this. First, regarding the website itself, you can just use what's easiest to you, or to the environment you're in. For example, a basic PHP page will do just fine, but if you can get a site running in Python as well, I'd prefer using the same language all over.
That said, I'm not sure why you would need to use a hosted website? Given that you're already forced to have a externally accessible PC at home for the communication, why not run a webserver on that directly (Apache, Nginx, or even something like CherryPy should do)? That webserver can then communicate with the python process that is running to control your Arduino (by using e.g. Python's xmlrpclib). If you would run things via the hosting company, you would still need some process that can handle external requests securely... something a webserver is quite good at. Just running it yourself gives you all the freedom you want, and simplifies things by lessening the number of components in your solution.
The updates on your site I'd keep quite basic: commands you want to run can be handled in the request handlers of the webserver by just calling the relevant (xmlrpclib) calls. Dynamically updating the page is best done by some AJAX calls I reckon. Based on your story, these updates are easily put in a JSON object, suitable for periodically updating only the relevant segments of your page.
I am currently working on a project to create simple file uploader site that will update the user of the progress of an upload.
I've been attempting this in pure python (with CGI) on the server side but to get the progress of the file I obviously need send requests to the server continually. I was looking to use AJAX to do this but I was wondering how hard it would be to, instead of changing to some other framerwork (web.py for instance), just write my own web server for receiving the XML HTTP Requests?
My main problem is that sending the request is done from HTML and Javascript so it all seems like magic trickery at the moment.
Can anyone advise me as to the best way to go about receiving these requests on the server?
EDIT: It seems that a framework would be the way to go. Would web.py be a good route to take?
I would recommend to use a microframework like Sinatra for Ruby. There seem to be some equivalents for Python. What python equivalent of Sinatra would you recommend?
Such a framework allows you to simply map a single method to a route.
Writing a very basic HTTP server won't be very hard (see http://docs.python.org/library/simplehttpserver.html for an example), but you will be missing many features that are provided by real servers and web frameworks.
For your project, I suggest you pick one of the many Python web frameworks and run your application behind Apache/mod_wsgi.
There's absolutely no need to write your own web server. Plenty of options exist, including lightweight ones like nginx.
You should use one of those, and either your own custom WSGI code to receive the request, or (better) one of the microframeworks like Flask or Bottle.