本文整理汇总了Python中PyQt5.QtNetwork.QNetworkAccessManager.setCookieJar方法的典型用法代码示例。如果您正苦于以下问题:Python QNetworkAccessManager.setCookieJar方法的具体用法?Python QNetworkAccessManager.setCookieJar怎么用?Python QNetworkAccessManager.setCookieJar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtNetwork.QNetworkAccessManager
的用法示例。
在下文中一共展示了QNetworkAccessManager.setCookieJar方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Fetch
# 需要导入模块: from PyQt5.QtNetwork import QNetworkAccessManager [as 别名]
# 或者: from PyQt5.QtNetwork.QNetworkAccessManager import setCookieJar [as 别名]
class Fetch():
data = QtCore.pyqtSignal(dict)
def __init__(self, parent):
self.session = QNetworkAccessManager(parent)
self.cookies = QNetworkCookieJar()
self.parent = parent
self.session.setCookieJar(self.cookies)
def base_handler(self, reply: QNetworkReply):
try:
response = json.loads(str(reply.readAll(), encoding='utf-8'))
status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
except:
self.parent.warn.add_warn("Http解析错误")
return
if reply.error() != QNetworkReply.NoError:
self.handler_error(response, status_code)
else:
self.data.emit(response)
def get(self, url, param=None):
url = QtCore.QUrl(parse_url(url, param))
request = QNetworkRequest(url)
reply = self.session.get(request)
return reply
def post(self, url, param=None, data=None, json=True):
if isinstance(data, dict):
f = ''
for i in data:
f += '{}={}&'.format(i, data[i])
data = f[:-1]
byte_data = QtCore.QByteArray()
byte_data.append(data)
url = QtCore.QUrl(parse_url(url, param))
request = QNetworkRequest(url)
if json:
request.setHeader(QNetworkRequest.ContentTypeHeader, 'application/json')
reply = self.session.post(request, byte_data)
return reply
def handler_error(self, response, status_code):
if isinstance(response, dict):
message = response.get('error', 'unknown')
self.parent.warn.add_warn('网络请求错误,错误码为{},原因为{}'.format(status_code, message))
示例2: __init__
# 需要导入模块: from PyQt5.QtNetwork import QNetworkAccessManager [as 别名]
# 或者: from PyQt5.QtNetwork.QNetworkAccessManager import setCookieJar [as 别名]
def __init__(self, parent):
super().__init__(parent)
self.set_url('http://google.ru')
conn = QNetworkAccessManager()
self.conn = conn
self.r = QNetworkRequest()
self.r.attribute(QNetworkRequest.CookieSaveControlAttribute, QVariant(True))
# self.r.setHeader(QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded")
# self.r.setRawHeader("Referer", "http://www.facebook.com/")
# self.r.setRawHeader("Host", "www.facebook.com")
self.cj = QNetworkCookieJar()
conn.setCookieJar(self.cj)
conn.createRequest = self._create_request
self.wv = WebView()
self.wv.show()
self.wv.page().setNetworkAccessManager(conn)
# self.wv.auth()
self.loop = QEventLoop()
pass