Python get HTTP
-
I am trying to accessing web resources.
Main.qmlimport 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); } } }
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.
-
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