UBports Robot Logo UBports Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Python get HTTP

    Scheduled Pinned Locked Moved Solved App Development
    2 Posts 1 Posters 268 Views 1 Watching
    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.
      • pavelprostoP Offline
        pavelprosto
        last edited by

        I am trying to accessing web resources.
        Main.qml

        import QtQuick 2.7
        import Ubuntu.Components 1.3
        //import QtQuick.Controls 2.2
        import QtQuick.Layouts 1.3
        import Qt.labs.settings 1.0
        import io.thp.pyotherside 1.3
        
        MainView {
            id: root
            objectName: 'mainView'
            applicationName: 'example-web.yourname'
            automaticOrientation: true
        
            width: units.gu(45)
            height: units.gu(75)
        
            Page {
                anchors.fill: parent
        
                header: PageHeader {
                    id: header
                    title: i18n.tr('Example Web')
                }
        
                Label {
                    id: label1
                    anchors {
                        top: header.bottom
                        left: parent.left
                        right: parent.right
                        bottom: parent.bottom
                    }
                    text: 'Get Web'
                    verticalAlignment: Label.AlignVCenter
                    horizontalAlignment: Label.AlignHCenter
                }
            }
        
            Python {
                id: python
                Component.onCompleted: {
                    addImportPath(Qt.resolvedUrl('../src/'));
        
                    importModule('example', function() {
                        python.call('example.speak',[], function(returnValue) {
                            console.log(returnValue);
                            label1.text=returnValue;
                        })
                    });
                }
        
                onError: {
                    console.log('python error: ' + traceback);
                }
            }
        }
        
        

        example.py

        import urllib.request
        import urllib3
        
        def speak():
          try:
            source = urllib.request.urlopen("http://webcode.me")
          except Exception as e:
            return str(e)
          else:
            html_bytes = source.read()
            html_str = html_bytes.decode("utf8")
            source.close()
            return html_str
        
        def speak2():
          http = urllib3.PoolManager()
          try:
            source = http.request('GET', 'http://webcode.me')
          except Exception as e:
            return str(e)
          else:
            html_str = source.data.decode('utf-8')
            return html_str
        

        Both examples work on a computer(clickable desktop), but when I run them on a phone, I get errors:
        on urllib:

        <urlopen error [Errno -3] Temporary failure in name resolution>
        

        on urllib3:

        HTTPConnectionPool(host='webcode.me', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xa8111390>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))
        

        How can fix it? =( Help me please.

        Your donations help me to continue programming free and openSource application development https://liberapay.com/pavelprosto/

        1 Reply Last reply Reply Quote 0
        • pavelprostoP Offline
          pavelprosto
          last edited by

          Find the * .apparmor file in the project folder.
          Add permissions:

          {
              "policy_groups": [
                  "content_exchange",
                  "content_exchange_source",
                  "networking",
                  "webview"
              ],
              "policy_version": 16.04
          }
          

          After that the program will work

          Your donations help me to continue programming free and openSource application development https://liberapay.com/pavelprosto/

          1 Reply Last reply Reply Quote 0
          • First post
            Last post