本文整理匯總了Python中PyQt5.QtNetwork.QNetworkRequest方法的典型用法代碼示例。如果您正苦於以下問題:Python QtNetwork.QNetworkRequest方法的具體用法?Python QtNetwork.QNetworkRequest怎麽用?Python QtNetwork.QNetworkRequest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PyQt5.QtNetwork
的用法示例。
在下文中一共展示了QtNetwork.QNetworkRequest方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def __init__(self, *args, **kwargs):
super(OpencvWidget, self).__init__(*args, **kwargs)
self.httpRequestAborted = False
self.fps = 24
self.resize(800, 600)
if not os.path.exists("Data/shape_predictor_68_face_landmarks.dat"):
self.setText("正在下載數據文件。。。")
self.outFile = QFile(
"Data/shape_predictor_68_face_landmarks.dat.bz2")
if not self.outFile.open(QIODevice.WriteOnly):
QMessageBox.critical(self, '錯誤', '無法寫入文件')
return
self.qnam = QNetworkAccessManager(self)
self._reply = self.qnam.get(QNetworkRequest(QUrl(URL)))
self._reply.finished.connect(self.httpFinished)
self._reply.readyRead.connect(self.httpReadyRead)
self._reply.downloadProgress.connect(self.updateDataReadProgress)
else:
self.startCapture()
示例2: __init__
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._stream_buffer = QByteArray()
self._stream_buffer_start_index = -1
self._network_manager = None # type: QNetworkAccessManager
self._image_request = None # type: QNetworkRequest
self._image_reply = None # type: QNetworkReply
self._image = QImage()
self._image_rect = QRect()
self._source_url = QUrl()
self._started = False
self._mirror = False
self.setAntialiasing(True)
示例3: test_dir
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def test_dir(self, tmpdir):
url = QUrl.fromLocalFile(str(tmpdir))
req = QNetworkRequest(url)
reply = filescheme.handler(req, None, None)
# The URL will always use /, even on Windows - so we force this here
# too.
tmpdir_path = str(tmpdir).replace(os.sep, '/')
assert reply.readAll() == filescheme.dirbrowser_html(tmpdir_path)
示例4: test_file
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def test_file(self, tmpdir):
filename = tmpdir / 'foo'
filename.ensure()
url = QUrl.fromLocalFile(str(filename))
req = QNetworkRequest(url)
reply = filescheme.handler(req, None, None)
assert reply is None
示例5: test_unicode_encode_error
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def test_unicode_encode_error(self, mocker):
url = QUrl('file:///tmp/foo')
req = QNetworkRequest(url)
err = UnicodeEncodeError('ascii', '', 0, 2, 'foo')
mocker.patch('os.path.isdir', side_effect=err)
reply = filescheme.handler(req, None, None)
assert reply is None
示例6: on_download_requested
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def on_download_requested(self, request):
"""Called when the user wants to download a link.
We need to construct a copy of the QNetworkRequest here as the
download_manager needs it async and we'd get a segfault otherwise as
soon as the user has entered the filename, as Qt seems to delete it
after this slot returns.
"""
req = QNetworkRequest(request)
download_manager = objreg.get('qtnetwork-download-manager')
download_manager.get_request(req, qnam=self.networkAccessManager())
示例7: acceptNavigationRequest
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def acceptNavigationRequest(self,
frame: QWebFrame,
request: QNetworkRequest,
typ: QWebPage.NavigationType) -> bool:
"""Override acceptNavigationRequest to handle clicked links.
Setting linkDelegationPolicy to DelegateAllLinks and using a slot bound
to linkClicked won't work correctly, because when in a frameset, we
have no idea in which frame the link should be opened.
Checks if it should open it in a tab (middle-click or control) or not,
and then conditionally opens the URL here or in another tab/window.
"""
type_map = {
QWebPage.NavigationTypeLinkClicked:
usertypes.NavigationRequest.Type.link_clicked,
QWebPage.NavigationTypeFormSubmitted:
usertypes.NavigationRequest.Type.form_submitted,
QWebPage.NavigationTypeFormResubmitted:
usertypes.NavigationRequest.Type.form_resubmitted,
QWebPage.NavigationTypeBackOrForward:
usertypes.NavigationRequest.Type.back_forward,
QWebPage.NavigationTypeReload:
usertypes.NavigationRequest.Type.reloaded,
QWebPage.NavigationTypeOther:
usertypes.NavigationRequest.Type.other,
}
is_main_frame = frame is self.mainFrame()
navigation = usertypes.NavigationRequest(url=request.url(),
navigation_type=type_map[typ],
is_main_frame=is_main_frame)
if navigation.navigation_type == navigation.Type.reloaded:
self.reloading.emit(navigation.url)
self.navigation_request.emit(navigation)
return navigation.accepted
示例8: requestHook
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def requestHook(self, request: QNetworkRequest):
super().requestHook(request)
token = self._account.accessToken
if not self._account.isLoggedIn or token is None:
Logger.warning("Cannot add authorization to Cloud Api request")
return
header_dict = {
"Authorization": "Bearer {}".format(token)
}
self.addHeaders(request, header_dict)
示例9: start
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def start(self) -> None:
self.stop() # Ensure that previous requests (if any) are stopped.
if not self._source_url:
Logger.log("w", "Unable to start camera stream without target!")
return
self._started = True
self._image_request = QNetworkRequest(self._source_url)
if self._network_manager is None:
self._network_manager = QNetworkAccessManager()
self._image_reply = self._network_manager.get(self._image_request)
self._image_reply.downloadProgress.connect(self._onStreamDownloadProgress)
示例10: requestHook
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def requestHook(self, request: QNetworkRequest) -> None:
"""Invoked after request-specific headers are set and before HttpRequestData is created"""
pass
示例11: addHeaders
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def addHeaders(request: QNetworkRequest, header_dict: Dict) -> None:
for key, value in header_dict.items():
request.setRawHeader(key.encode("utf-8"), value.encode("utf-8"))
示例12: make_request
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def make_request(self, url, data, filename):
print(f"Making request to {url}")
# Create the request object
self.request = qtn.QNetworkRequest(url)
# create the multipart object
self.multipart = qtn.QHttpMultiPart(qtn.QHttpMultiPart.FormDataType)
# Write the key-value data to the multipart
for key, value in (data or {}).items():
http_part = qtn.QHttpPart()
http_part.setHeader(
qtn.QNetworkRequest.ContentDispositionHeader,
f'form-data; name="{key}"'
)
http_part.setBody(value.encode('utf-8'))
self.multipart.append(http_part)
# Write the file data to the multipart
if filename:
file_part = qtn.QHttpPart()
filedata = open(filename, 'rb').read()
file_part.setHeader(
qtn.QNetworkRequest.ContentDispositionHeader,
f'form-data; name="attachment"; filename="{filename}"'
)
file_part.setBody(filedata)
self.multipart.append(file_part)
# Post the request with the form data
self.nam.post(self.request, self.multipart)
示例13: make_request
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def make_request(self, url, data, filename):
print(f"Making request to {url}")
# Create the request object
self.request = qtn.QNetworkRequest(url)
# create the multipart object
self.multipart = qtn.QHttpMultiPart(qtn.QHttpMultiPart.FormDataType)
# Write the key-value data to the multipart
json_string = json.dumps(data)
http_part = qtn.QHttpPart()
http_part.setHeader(
qtn.QNetworkRequest.ContentTypeHeader,
'text/json'
)
http_part.setBody(json_string.encode('utf-8'))
self.multipart.append(http_part)
# Write the file data to the multipart
if filename:
file_part = qtn.QHttpPart()
filedata = open(filename, 'rb').read()
file_part.setHeader(
qtn.QNetworkRequest.ContentDispositionHeader,
f'form-data; name="attachment"; filename="{filename}"'
)
file_part.setBody(filedata)
self.multipart.append(file_part)
# Post the request with the form data
self.nam.post(self.request, self.multipart)
開發者ID:PacktPublishing,項目名稱:Mastering-GUI-Programming-with-Python,代碼行數:33,代碼來源:question_5_json_poster.py
示例14: __init__
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def __init__(self, url):
super().__init__()
self.manager = qtn.QNetworkAccessManager(
finished=self.on_finished)
self.request = qtn.QNetworkRequest(qtc.QUrl(url))
self.manager.get(self.request)
示例15: __init__
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QNetworkRequest [as 別名]
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._stream_buffer = QByteArray()
self._stream_buffer_start_index = -1
self._network_manager = None # type: QNetworkAccessManager
self._image_request = None # type: QNetworkRequest
self._image_reply = None # type: QNetworkReply
self._image = QImage()
self._image_rect = QRect()
self._source_url = QUrl()
self._started = False
self._mirror = False
self.setAntialiasing(True)
## Ensure that close gets called when object is destroyed