Last Updated on 2021-06-02 by Clay
I wrote a simple socket tutorial before, if you are not similar about this, maybe you can refer here: Python Package Tutorial: Socket
Just like I mentioned in the above article, I want to use Socket to build a simple chatbot that will repeat the sentence you talk to it. (Of course, you can set any topic you want!)
Let's start!
If you want to know the programming language "Python", maybe you can refer here: https://docs.python.org/3/howto/sockets.html
Server.py
# -*- coding: utf-8 -*- import json import socket # HOST & PORT with open('HOST_PORT.json', 'r', encoding='utf-8') as f: HOST_PORT = json.load(f) HOST = HOST_PORT['HOST'] PORT = int(HOST_PORT['PORT']) server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((HOST, PORT)) server.listen(5) while True: conn, addr = server.accept() clientMessage = str(conn.recv(1024), encoding='utf-8') print('Client message is:', clientMessage) # Repeat serverMessage = clientMessage conn.sendall(serverMessage.encode()) conn.close()
The server port I set it in my own server machine, to avoid to leak my ip, I use "json" to load the sensitive information. You just need to fill your own server ip by string data type.
GUI
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'socket_chatbot.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(981, 749) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget) self.textBrowser.setGeometry(QtCore.QRect(0, 0, 981, 591)) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) font.setBold(True) font.setWeight(75) self.textBrowser.setFont(font) self.textBrowser.setObjectName("textBrowser") self.textEdit = QtWidgets.QTextEdit(self.centralwidget) self.textEdit.setGeometry(QtCore.QRect(3, 600, 701, 87)) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) font.setBold(True) font.setWeight(75) self.textEdit.setFont(font) self.textEdit.setObjectName("textEdit") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(720, 595, 231, 101)) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(28) font.setBold(True) font.setWeight(75) self.pushButton.setFont(font) self.pushButton.setStyleSheet("color: rgb(255, 255, 255);\n" "background-color: rgb(220, 0, 0);\n"
The chatbot UI I was design by Qt Designer. If you have a bit interested, you can refer my note: PyQt5 Tutorial (1) Install PyQt5 and print “Hello World!”
Client.py
# -*- coding: utf-8 -*- import sys import json import socket from PyQt5 import QtWidgets from socket_chatbot import Ui_MainWindow # HOST & PORT with open('HOST_PORT.json', 'r', encoding='utf-8') as f: HOST_PORT = json.load(f) class MainWindow(QtWidgets.QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.textBrowser.append('<font color="#FF0000">Server: Hello! I will repeat what you said!</font>') self.HOST = HOST_PORT['HOST'] self.PORT = int(HOST_PORT['PORT']) self.ui.pushButton.clicked.connect(self.buttonEvent) def buttonEvent(self): text = self.ui.textEdit.toPlainText() self.ui.textEdit.clear() self.ui.textBrowser.append('User: '+text) self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.client.connect((self.HOST, self.PORT)) self.client.sendall(text.encode()) # Server message serverMessage = str(self.client.recv(1024), encoding='utf-8') self.ui.textBrowser.append('<font color="#FF0000">Server: '+serverMessage+'</font>') self.client.close() if __name__ == '__main__': app = QtWidgets.QApplication([]) window = MainWindow() window.show() sys.exit(app.exec_())
Output:
try this multilayered socket programming in python….
http://net-informations.com/python/net/thread.htm