pyqt5 笔记实现http请求发送
Posted 蘑菇の甜酱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt5 笔记实现http请求发送相关的知识,希望对你有一定的参考价值。
上个图~
index.py 文件
1 # -*- coding: utf-8 -*- 2 from PyQt5 import QtWidgets,QtCore #从pyqt库导入QtWindget通用窗口类 3 from form import Ui_form 4 from url import Url 5 import chardet 6 import json 7 from PyQt5.QtWidgets import (QWidget,QTableWidget,QHBoxLayout,QApplication,QDesktopWidget,QTableWidgetItem,QHeaderView) 8 9 class mywindow(QtWidgets.QWidget,Ui_form): #自己建的类,继承QtWidgets.Qwidget类方法和Ui_Form界面类 10 11 _signal = QtCore.pyqtSignal(list,tuple,str) #定义信号,定义参数类型为str 12 13 def __init__(self): 14 super(mywindow,self).__init__() #首先找到子类(mywindow)的父类(QWidget),然后把my的对象self转成QWidget的对象,然后被转化的self调用自己的init函数 15 self.setupUi(self) #直接继承界面类,调用类的setupUi方法 16 17 self.submit.clicked.connect(self.mySubmit) #连接自己的槽函数 18 self._signal.connect(self.myPrint) #将信号连接到函数mySignal 19 20 def mySubmit(self): #自定义的槽函数。槽其实就是个函数(方法) 21 text = self.url.text() #获取值 22 #combox = self.type.currentText() #获取复选框的值 23 tbtext = self.tb1.toPlainText() #获取输入框的值 24 tbtext = tbtext.split(\'\\n\') #根据换行符 截断 25 bdtext = self.bd.toPlainText() 26 new=Url(text,tbtext,bdtext) #发送给url文件,调用发请求的函数 27 choose = self.tp.currentText() 28 if choose == \'GET\': 29 #print(\'get lo~\') 30 result = new.get() 31 else: 32 #print(\'post lo~\') 33 result = new.post() 34 35 he = result[\'he\'] #响应头 36 st = result[\'st\'] #状态码 37 bo = result[\'bo\'].decode(\'utf-8\') #响应体,转化为unicode 38 #bo = str(json.loads(bo)) #unicode进行json解析 39 40 #print(type(he)) #查看他们的类型 41 #print(type(st)) 42 #print(type(bo)) 43 self._signal.emit(he,st,bo) 44 45 def myPrint(self,he,st,bo): #自定义信号函数 46 self.tb.append(bo) 47 st = str(st) 48 self.tb2.append(st) 49 for i in he: 50 self.tb2.append(str(i)) 51 #self.tb2.append(he) 52 #print(he) 53 #for k, v in f.getheaders(): #响应头 54 # print(\'%s: %s\' % (k, v)) 55 56 57 58 if __name__=="__main__": 59 import sys 60 61 app=QtWidgets.QApplication(sys.argv) #pyqt窗口必须在QApplication方法中使用 62 myshow=mywindow() #生成mywindow类的实例 myshow 63 myshow.show() #myshow调用show方法 64 sys.exit(app.exec()) #消息结束的时候,结束进程,并返回0,接着调用sys.exit(0)退出程序
form.py 文件
1 # -*- coding: utf-8 -*- 2 3 # Form implementation generated from reading ui file \'form.ui\' 4 # 5 # Created by: PyQt5 UI code generator 5.4.1 6 # 7 # WARNING! All changes made in this file will be lost! 8 9 from PyQt5 import QtCore, QtGui, QtWidgets 10 11 class Ui_form(object): 12 def setupUi(self, form): 13 form.setObjectName("form") 14 form.resize(1126, 656) 15 self.quit = QtWidgets.QPushButton(form) 16 self.quit.setGeometry(QtCore.QRect(460, 50, 75, 23)) 17 self.quit.setObjectName("quit") 18 self.submit = QtWidgets.QPushButton(form) 19 self.submit.setGeometry(QtCore.QRect(120, 50, 75, 23)) 20 self.submit.setObjectName("submit") 21 self.url = QtWidgets.QLineEdit(form) 22 self.url.setGeometry(QtCore.QRect(30, 19, 501, 21)) 23 self.url.setText("") 24 self.url.setObjectName("url") 25 self.tb = QtWidgets.QTextEdit(form) 26 self.tb.setGeometry(QtCore.QRect(480, 260, 621, 381)) 27 self.tb.setObjectName("tb") 28 self.label = QtWidgets.QLabel(form) 29 self.label.setGeometry(QtCore.QRect(30, 110, 91, 16)) 30 self.label.setObjectName("label") 31 self.label_2 = QtWidgets.QLabel(form) 32 self.label_2.setGeometry(QtCore.QRect(40, 380, 141, 16)) 33 font = QtGui.QFont() 34 font.setFamily("黑体") 35 font.setPointSize(10) 36 font.setBold(False) 37 font.setWeight(50) 38 self.label_2.setFont(font) 39 self.label_2.setObjectName("label_2") 40 self.label_3 = QtWidgets.QLabel(form) 41 self.label_3.setGeometry(QtCore.QRect(590, 50, 121, 16)) 42 self.label_3.setObjectName("label_3") 43 self.tb1 = QtWidgets.QTextEdit(form) 44 self.tb1.setGeometry(QtCore.QRect(30, 190, 391, 171)) 45 self.tb1.setObjectName("tb1") 46 self.tb2 = QtWidgets.QTextEdit(form) 47 self.tb2.setGeometry(QtCore.QRect(590, 80, 511, 171)) 48 self.tb2.setObjectName("tb2") 49 self.label_4 = QtWidgets.QLabel(form) 50 self.label_4.setGeometry(QtCore.QRect(30, 140, 261, 31)) 51 font = QtGui.QFont() 52 font.setFamily("黑体") 53 font.setPointSize(10) 54 font.setBold(False) 55 font.setWeight(50) 56 self.label_4.setFont(font) 57 self.label_4.setObjectName("label_4") 58 self.bd = QtWidgets.QTextEdit(form) 59 self.bd.setGeometry(QtCore.QRect(30, 410, 391, 181)) 60 self.bd.setObjectName("bd") 61 self.tp = QtWidgets.QComboBox(form) 62 self.tp.setGeometry(QtCore.QRect(30, 50, 69, 22)) 63 self.tp.setObjectName("tp") 64 self.tp.addItem("") 65 self.tp.addItem("") 66 self.label_5 = QtWidgets.QLabel(form) 67 self.label_5.setGeometry(QtCore.QRect(490, 230, 121, 16)) 68 self.label_5.setObjectName("label_5") 69 70 self.retranslateUi(form) 71 self.quit.clicked.connect(form.close) 72 QtCore.QMetaObject.connectSlotsByName(form) 73 74 def retranslateUi(self, form): 75 _translate = QtCore.QCoreApplication.translate 76 form.setWindowTitle(_translate("form", "Form")) 77 self.quit.setText(_translate("form", "Quit")) 78 self.submit.setText(_translate("form", "emit")) 79 self.label.setText(_translate("form", "Request")) 80 self.label_2.setText(_translate("form", "输入Request的body~")) 81 self.label_3.setText(_translate("form", "Response Header")) 82 self.tb1.sethtml(_translate("form", "<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.0//EN\\" \\"http://www.w3.org/TR/REC-html40/strict.dtd\\">\\n" 83 "<html><head><meta name=\\"qrichtext\\" content=\\"1\\" /><style type=\\"text/css\\">\\n" 84 "p, li { white-space: pre-wrap; }\\n" 85 "</style></head><body style=\\" font-family:\\\'SimSun\\\'; font-size:9pt; font-weight:400; font-style:normal;\\">\\n" 86 "<p style=\\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\\">Connection:keep-alive</p>\\n" 87 "<p style=\\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\\">Cache-Control: max-age=0</p>\\n" 88 "<p style=\\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\\">User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36</p></body></html>")) 89 self.label_4.setText(_translate("form", "输入Request的Header,以换行分割~")) 90 self.tp.setItemText(0, _translate("form", "GET")) 91 self.tp.setItemText(1, _translate("form", "POST")) 92 self.label_5.setText(_translate("form", "Response Body"))
xampp的php文件
<?php
echo \'Welcome~:\',$_POST[\'username\']
?>
以上是关于pyqt5 笔记实现http请求发送的主要内容,如果未能解决你的问题,请参考以下文章