Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Intro and question re Google App Engine and web stuff in Pythonista
-
Hi, new member, old programmer. Just starting with Pythonista (and Python). I need to write a very simple web service kind of thing for Google App Engine. Can do it with the Google launcher on my Mac, of course, but most days I just do things on my iPad.
The app just takes an http query line like?add,key=foo&val=bar and parses it down, creates a record, stores in App Engine, other queries read records and other fairly simple stuff. I see there's some good parsing stuff in Python, just the thing for what I need. So I can clearly do some of the prep work in Pythonista and that's cool.
However I could use some pointers on these things:
First, can I set up a little HTML page, have Pythonista server it and run my Python code in response to it, in the internal browser, or in Safari? If so, a pointer to an example would be helpful: I've not found one yet.
Second, is there a unit-testing framework here? I see there is a package unittest, haven't tried loading it as so far all I've done is run a couple of the samples and modify them.
Very early days with Pythonista for me and I promise to do my homework but there's a lot to take in and a couple of pointers, especially to simple examples, would be great. Thanks!
-
Woot, ran unittest demo:
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2)
if name == 'main':
unittest.main()life is good so far ...
-
@ronjeffries , welcome. Sorry, I can't help you with your questions. I thought I would just write this now while the forum is quite.
But if you look at the requests module in the help file. Is a high level lib to connect to sites and request data etc. there are also the low level Python libs for doing that. (Also a search in the forum for requests)
Also Pythonista has libs bottle and flask included. Which is also worth a look at.
As I say, I can't help. But I am sure these topics will be of interest to you. -
@ronjeffries Welcome! You cannot write Google App Engine apps in Pythonista (or at least, you can't run them locally). As @Phuket2 already pointed out though, Pythonista does include two pretty popular frameworks/modules for developing web apps: Flask and Bottle. When you use those, it's absolutely possible to run a local web server and view the pages in the built-in browser. You can also view your web app in Safari, but only for a couple of minutes (because Pythonista cannot run in the background indefinitely).
-
Thanks @Phuket2 and @omz ... i will look up the requests module right away.
yes i know i can't get all the way to writing or running the app engine stuff. what i'm thinking i might do is roughly this:
get a server running in Pythonista on some port; get a client set up to talk to that port; debug the protocol i plan to use with app engine and the parsing and such that I do.
i've now experimented with some of the server / client examples in the Python docs and with a little demo I found here. They sort of worked :) what would help would be a very simple example of a server.py and client.py that talk to each other.
what i'm not seeing right now is more about Pythonista itself. suppose i have those two scripts. How do i get them to run at the same time, and talk to each other? I've not figured out how to get two tabs worth of code in the same "run". So at least some of my trouble is I'm so new I don't get the drift of how Pythonista itself is set up. Happy to be told to "go read X", preferably with a decent value of X that i can find.
Thanks,
-
Ah, begins to look like I can't really run a server and a client at the same time with Pythonista. I guess what I'll do is just rig up a class or function and call it with unittest to get it working, paste it into the internet on the Mac.
If I'm missing some obvious idea let me know ... thanks!
-
@ronjeffries , I am not into the stuff you are talking about. But I would like to be, but still beyond me.
But I have been talking before how to have a local setup up before with bottle, a client and server for testing.
@omz also has another product called Editorial, which can also execute Python. So was able to start a bottle app in Editorial and send requests to it from Pythonista. There were also talks about using theads inside Pythonista to try to both at the same time. I think a difficult situation. Not saying you should buy Editorial, but is an interesting way to make a client/server test app.
But if you search the forum bottle and Editorial, I think you would get the discussion threads -
@ronjeffries , this is a good read about ruining bottle
https://forum.omz-software.com/topic/2451/running-bottle-in-pythonista-and-another-script-also -
Thanks @Phuket2 I'll check that out.
Here's my new problem, I have this code
# coding: utf-8 # example from https://docs.python.org/2/library/socketserver.html import SocketServer class MyTCPHandler(SocketServer.BaseRequestHandler): """ The request handler class for our server. It is instantiated once per connection to the server, and must override the handle() method to implement communication to the client. """ def handle(self): # self.request is the TCP socket connected to the client self.data = self.request.recv(1024).strip() print "ron jeffries has gotten something working part 2" print "{} wrote:".format(self.client_address[0]) print self.data # just send back the same data, but upper-cased back = "here you go\n" + self.data.upper() self.request.sendall(back) if __name__ == "__main__": HOST, PORT = "localhost", 9999 # Create the server, binding to localhost on port 9999 server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever()
So when I run this code, the server is running, and if I go into Safari I can send like
localhost:9999/?add&key=k1&val=v1
And get back sensible stuff from my server. So my soon-to-be app engine code would be in the server above. Which means I need to stop it, modify it, start it up again, then test again on the Safari side.
box to stop the thing, edit it, and restart it, I get the message[Errno 48] Address already in use.
I can stop Pythonista and restart it to run again but that seems wrong. So how do I, within Pythonista, stop this server, edit it, and restart it on the same address?
Thanks!
-
@ronjeffries , as I say I can only talk about this subject in very very and very broad strokes. I have no chance to help on this on. Someone else will come along though that can.
-
@ronjeffries You can get rid of the error like this:
# ... if __name__ == "__main__": # Probably not strictly necessary: SocketServer.TCPServer.allow_reuse_address = True HOST, PORT = "localhost", 9999 # Create the server, binding to localhost on port 9999 server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C try: server.serve_forever() except KeyboardInterrupt: # Clean shutdown: server.shutdown() server.socket.close()
-
For some reason when I run a web server with Pythonista, stop it, then try to restart, I get the error message once, but the next time I run the script (without restarting Pythonista) it's gone and the server runs again. Don't ask me why that happens...
-
Thanks @omz, I'll try that ... in other news, how do i do a control-C in Pythonista? Thanks!
-
@omz Should
server.shutdown
beserver.shutdown()
? Not sure.@ronjeffries You can run two scripts at once using threads. Take a look at the
threading
module. Running something on a thread will not prevent you from using the interpreter or from performing other tasks while it runs. -
Tapping the "Stop" button is essentially the same as Ctrl+C.
Should server.shutdown be server.shutdown()? Not sure.
Yes, thanks.
-
Thanks @omz I had hoped that was the case.
Thanks @Webmaster4o maybe I'll have a look at that, though I am thinking that I need to go another way and maybe talk to the thing from Safari or something. Or maybe just work on the core in Pythonista and then do the real work with the App Engine launcher on the Mac.Thanks!
-
Another option to navigate to a page you have created is to use the webbrowser module, which will open the page in a separate tab inisde pythonista, which runs in its own thread.