Detect all the network erroneous cases with Qt
-
detect all the network erroneous cases with Qt
I want to get Page Source by the following code ,I wonder is it enough to detect all the network erroneous cases with the following code ?
@
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtNetwork import *
import chardetdef showSrc(reply):
redirctLocation=reply.header(QNetworkRequest.LocationHeader)
realUrl=reply.url() if not redirctLocation else redirctLocation
print(realUrl)if (reply.error()!= QNetworkReply.NoError): print('11111111', reply.errorString()) return content=reply.readAll().data() if content==b'': print('---------', 'cannot find any resource !') return charCodec = chardet.detect(content)['encoding'] webPageSRC = content.decode(charCodec) print(webPageSRC ) qApp.quit()
if name == 'main':
app =QCoreApplication(sys.argv) manager=QNetworkAccessManager () url =input('input url :') request=QNetworkRequest (QUrl.fromEncoded(QUrl.fromUserInput(url).toEncoded())) request.setRawHeader("User-Agent" ,'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 SE 2.X MetaSr 1.0') reply=manager.get(request) manager.finished.connect(showSrc) app.exec()
@
-
Enough to do what? You will get every error because you check on !NoError. What else can happen then?
Please be a bit more specific in your questions. It is hard to help when information is missing!