Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created March 24, 2021 12:47
Show Gist options
  • Save ThomasG77/85b65534beca578b8eab3826e50f0637 to your computer and use it in GitHub Desktop.
Save ThomasG77/85b65534beca578b8eab3826e50f0637 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
'''
QNetworkAccessManager in PyQt
In this example we get a web page.
Author: Jan Bodnar
Website: zetcode.com
'''
from PyQt5 import QtNetwork
from PyQt5.QtCore import QCoreApplication, QUrl
import sys
class Example:
def __init__(self):
self.doRequest()
def doRequest(self):
url = 'https://api.mapbox.com/valhalla/v1'
req = QtNetwork.QNetworkRequest(QUrl(url))
self.nam = QtNetwork.QNetworkAccessManager()
self.nam.finished.connect(self.handleResponse)
self.nam.get(req)
def handleResponse(self, reply):
er = reply.error()
if er == QtNetwork.QNetworkReply.NoError:
bytes_string = reply.readAll()
print(str(bytes_string, 'utf-8'))
else:
print("Error occured: ", er)
print(reply.errorString())
bytes_string = reply.readAll()
print(str(bytes_string, 'utf-8'))
QCoreApplication.quit()
def main():
app = QCoreApplication([])
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment