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.


    A Simple HTTP Server

    Pythonista
    10
    22
    29147
    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.
    • omz
      omz last edited by

      If you want to download files in Pythonista to your Mac or PC or are maybe just interested in learning a little more about Python's networking modules, you might find this little script useful:

      https://gist.github.com/3823483
      (use the <a href="http://omz-software.com/pythonista/forums/discussion/5/sharing-code-on-github#Item_1">New from Gist</a> script to import it easily)

      It shows how to create a basic HTTP server that runs on your iPad/iPhone and allows you to download/upload files from/to Pythonista, using a regular web browser on a different machine.

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

        Very useful script. I use everyday but I need to change the url name of the Ipad to the IP address. i.e.: http://123.456.1.81:8080

        That only works in my home wifi network where I know the IP address but not works in another wifi network.

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

          import socket
          ipAddr = socket.gethostbyname(socket.gethostname()) # incorrect!!  See below.
          

          This might work but I don't have time now to check it out.

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

            Thank you ccc. But i have the error:

            nodename nor servname provided, or not known

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

              socket.gethostname() and socket.getfqdn() both return the name of the iPad, such as 'Johns-iPad'. This name is not going to be in DNS, which is why you're getting 'nodename ... not known'

              The way to turn the name into something that can be looked up is to append '.local' as omz does in his script. This allows the name to be looked up via Bonjour/mdns.

              If this isn't working for you, my first thought is to go to the Settings app and look up your IP address under the Wi-Fi section by tapping the arrow next to your current network.

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

                Thanks achorrath for catching my error...

                import socket
                ipAddr = socket.gethostbyname(socket.gethostname() + '.local') # corrected!
                # The .local trick only works on Bonjour/mDNS networks.
                
                1 Reply Last reply Reply Quote 0
                • jose3f23
                  jose3f23 last edited by

                  With this line
                  <code>ipAddr = socket.gethostbyname(socket.gethostname() + '.local') </code>

                  Remains the error:
                  socket.gaierror: [Errno 8] nodename nor servname provided, or not known

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

                    You are not on a mDNS network.

                    Is the box that you are using a Mac, Linux, or Windoze?
                    <li>Mac should just work. See http://www.apple.com/support/bonjour</li>
                    <li>Linux should just work. See http://en.wikipedia.org/wiki/.local</li>
                    <li>Windoze you can try to add http://support.apple.com/kb/DL999 but it may or may not help.</li>

                    http://en.wikipedia.org/wiki/Multicast_DNS

                    http://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Conceptual/NetServices/Articles/about.html

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

                      Thank you @ccc and @achorrath.

                      The problem I have is that the name of my iPad that appears in the script can not be recognised by the browser in the Mac. I know the problem is related to the DNS not having the Ipad name.

                      I would like that the script shows the message with the Ipad Ip address. Of course a circumvent is to go to Settings>Wifi and look up for the IP address. This is that I do everyday.

                      @ccc: nice collection of useful links.

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

                        This should work for printing the IP address:

                        import socket
                        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                        s.connect(('8.8.8.8',80))
                        print s.getsockname()[0]
                        s.close()
                        

                        (From this answer on StackOverflow)

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

                          @omc: endlich....

                          Now it works.

                          Thanks

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

                            server.serve_forever()
                            

                            iOS automatically stops background app about 10 minutes.
                            Is there any way to run pythonista server script "forever" (over 24 hours) in background?

                            1 Reply Last reply Reply Quote 1
                            • MartinPacker
                              MartinPacker last edited by

                              Thanks for asking that question @duffy . It's what ultimately inhibited my using a Pythonista HTTP Server to provide a web page in slide over that acts as a toolbar.

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

                                @duffy @MartinPacker
                                I think this kind of questions has been asked before. check here, or here.

                                It seems that @ccc has a good solution:

                                console.set_idle_timer_disabled(flag)
                                

                                I haven't tried that myself so I'm not sure it will work in that particular case, but give it a shot!

                                duffy 1 Reply Last reply Reply Quote 0
                                • duffy
                                  duffy @cook last edited by

                                  @cook

                                  console.set_idle_timer_disabled(True)
                                  

                                  I tried it. But this have effect only in the foreground.

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

                                    You could try no_doze.py but it is not pretty.

                                    duffy 1 Reply Last reply Reply Quote 0
                                    • duffy
                                      duffy @ccc last edited by

                                      @ccc
                                      I tried no_doze.py in iOS9. but it did not relaunch self. I think iOS changed behavior.

                                      reincarnate(argv)  # Silent notification can relaunch self
                                      
                                      1 Reply Last reply Reply Quote 0
                                      • brumm
                                        brumm last edited by brumm

                                        You can try this

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

                                          @brumm That got removed from the released version in app review.

                                          If you want your app to live, it needs to be in the foreground.
                                          I believe we can use
                                          beginBackgroundTaskWithExpirationHandler
                                          from the main app to run code when we area about to be killed. Also, app.backgroundTimeRemaining does give remaining time, though not sure if this is a guaranteed kill when the timer reaches 0

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

                                            @JonB It's redundant to call beginBackgroundTaskWithExpirationHandler yourself; Pythonista already does that when you run a script (otherwise, the app would be suspended immediately when in the background), and you don't get more background time by starting multiple background tasks.

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