您的位置:首页 > 编程语言 > Qt开发

pyqt5 笔记(二)实现http请求发送

2016-06-30 15:38 537 查看
上个图~



index.py 文件

# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets,QtCore                        #从pyqt库导入QtWindget通用窗口类
from form import Ui_form
from url import Url
import chardet
import json
from PyQt5.QtWidgets import (QWidget,QTableWidget,QHBoxLayout,QApplication,QDesktopWidget,QTableWidgetItem,QHeaderView)

class mywindow(QtWidgets.QWidget,Ui_form):                  #自己建的类,继承QtWidgets.Qwidget类方法和Ui_Form界面类

_signal = QtCore.pyqtSignal(list,tuple,str)                #定义信号,定义参数类型为str

def __init__(self):
super(mywindow,self).__init__()             #首先找到子类(mywindow)的父类(QWidget),然后把my的对象self转成QWidget的对象,然后被转化的self调用自己的init函数
self.setupUi(self)                          #直接继承界面类,调用类的setupUi方法

self.submit.clicked.connect(self.mySubmit)       #连接自己的槽函数
self._signal.connect(self.myPrint)               #将信号连接到函数mySignal

def mySubmit(self):                                     #自定义的槽函数。槽其实就是个函数(方法)
text = self.url.text()                      #获取值
#combox = self.type.currentText()           #获取复选框的值
tbtext = self.tb1.toPlainText()             #获取输入框的值
tbtext = tbtext.split('\n')                 #根据换行符 截断
bdtext = self.bd.toPlainText()
new=Url(text,tbtext,bdtext)                       #发送给url文件,调用发请求的函数
choose = self.tp.currentText()
if choose == 'GET':
#print('get lo~')
result = new.get()
else:
#print('post lo~')
result = new.post()

he = result['he']                       #响应头
st = result['st']                       #状态码
bo = result['bo'].decode('utf-8')             #响应体,转化为unicode
#bo = str(json.loads(bo))                   #unicode进行json解析

#print(type(he))                        #查看他们的类型
#print(type(st))
#print(type(bo))
self._signal.emit(he,st,bo)

def myPrint(self,he,st,bo):                                  #自定义信号函数
self.tb.append(bo)
st = str(st)
self.tb2.append(st)
for i in he:
self.tb2.append(str(i))
#self.tb2.append(he)
#print(he)
#for k, v in f.getheaders():             #响应头
#    print('%s: %s' % (k, v))

if __name__=="__main__":
import sys

app=QtWidgets.QApplication(sys.argv)            #pyqt窗口必须在QApplication方法中使用
myshow=mywindow()                               #生成mywindow类的实例 myshow
myshow.show()                                   #myshow调用show方法
sys.exit(app.exec())                            #消息结束的时候,结束进程,并返回0,接着调用sys.exit(0)退出程序


form.py 文件

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'form.ui'
#
# Created by: PyQt5 UI code generator 5.4.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_form(object):
def setupUi(self, form):
form.setObjectName("form")
form.resize(1126, 656)
self.quit = QtWidgets.QPushButton(form)
self.quit.setGeometry(QtCore.QRect(460, 50, 75, 23))
self.quit.setObjectName("quit")
self.submit = QtWidgets.QPushButton(form)
self.submit.setGeometry(QtCore.QRect(120, 50, 75, 23))
self.submit.setObjectName("submit")
self.url = QtWidgets.QLineEdit(form)
self.url.setGeometry(QtCore.QRect(30, 19, 501, 21))
self.url.setText("")
self.url.setObjectName("url")
self.tb = QtWidgets.QTextEdit(form)
self.tb.setGeometry(QtCore.QRect(480, 260, 621, 381))
self.tb.setObjectName("tb")
self.label = QtWidgets.QLabel(form)
self.label.setGeometry(QtCore.QRect(30, 110, 91, 16))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(form)
self.label_2.setGeometry(QtCore.QRect(40, 380, 141, 16))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(form)
self.label_3.setGeometry(QtCore.QRect(590, 50, 121, 16))
self.label_3.setObjectName("label_3")
self.tb1 = QtWidgets.QTextEdit(form)
self.tb1.setGeometry(QtCore.QRect(30, 190, 391, 171))
self.tb1.setObjectName("tb1")
self.tb2 = QtWidgets.QTextEdit(form)
self.tb2.setGeometry(QtCore.QRect(590, 80, 511, 171))
self.tb2.setObjectName("tb2")
self.label_4 = QtWidgets.QLabel(form)
self.label_4.setGeometry(QtCore.QRect(30, 140, 261, 31))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.bd = QtWidgets.QTextEdit(form)
self.bd.setGeometry(QtCore.QRect(30, 410, 391, 181))
self.bd.setObjectName("bd")
self.tp = QtWidgets.QComboBox(form)
self.tp.setGeometry(QtCore.QRect(30, 50, 69, 22))
self.tp.setObjectName("tp")
self.tp.addItem("")
self.tp.addItem("")
self.label_5 = QtWidgets.QLabel(form)
self.label_5.setGeometry(QtCore.QRect(490, 230, 121, 16))
self.label_5.setObjectName("label_5")

self.retranslateUi(form)
self.quit.clicked.connect(form.close)
QtCore.QMetaObject.connectSlotsByName(form)

def retranslateUi(self, form):
_translate = QtCore.QCoreApplication.translate
form.setWindowTitle(_translate("form", "Form"))
self.quit.setText(_translate("form", "Quit"))
self.submit.setText(_translate("form", "emit"))
self.label.setText(_translate("form", "Request"))
self.label_2.setText(_translate("form", "输入Request的body~"))
self.label_3.setText(_translate("form", "Response Header"))
self.tb1.setHtml(_translate("form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<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"
"<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"
"<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>"))
self.label_4.setText(_translate("form", "输入Request的Header,以换行分割~"))
self.tp.setItemText(0, _translate("form", "GET"))
self.tp.setItemText(1, _translate("form", "POST"))
self.label_5.setText(_translate("form", "Response Body"))


xampp的php文件

<?php
echo 'Welcome~:',$_POST['username']
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: