omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    6
    17
    8846
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Phuket2
      Phuket2 @ronjeffries last edited by

      @ronjeffries , this is a good read about ruining bottle
      https://forum.omz-software.com/topic/2451/running-bottle-in-pythonista-and-another-script-also

      1 Reply Last reply Reply Quote 0
      • ronjeffries
        ronjeffries last edited by

        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!

        Phuket2 1 Reply Last reply Reply Quote 0
        • Phuket2
          Phuket2 @ronjeffries last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • omz
            omz last edited by omz

            @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()
            
            1 Reply Last reply Reply Quote 0
            • dgelessus
              dgelessus last edited by

              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...

              1 Reply Last reply Reply Quote 0
              • ronjeffries
                ronjeffries last edited by

                Thanks @omz, I'll try that ... in other news, how do i do a control-C in Pythonista? Thanks!

                1 Reply Last reply Reply Quote 0
                • Webmaster4o
                  Webmaster4o last edited by

                  @omz Should server.shutdown be server.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.

                  1 Reply Last reply Reply Quote 0
                  • omz
                    omz last edited by

                    @ronjeffries

                    Tapping the "Stop" button is essentially the same as Ctrl+C.

                    @Webmaster4o

                    Should server.shutdown be server.shutdown()? Not sure.

                    Yes, thanks.

                    1 Reply Last reply Reply Quote 0
                    • ronjeffries
                      ronjeffries last edited by

                      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!

                      1 Reply Last reply Reply Quote 0
                      • JonB
                        JonB last edited by

                        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.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors