Use database to create couple's remote communication program

1. Function introduction:

Rent a server and use the server as an information transmission intermediary to realize information communication between different computers.

2. Why rent a server as an intermediary? Is it not possible to directly communicate information between two devices?

Because the IP address of the rented server is fixed, and the IP address of personal devices (such as mobile phones and personal computers) changes at any time, if the IP is written into the program, an IP transformation and modification program will be required later, which is obviously very impractical. .

3. Implementation steps

1. Rent a server and create a database

2. Use python to connect to the database

3. Log in to the corresponding account, send data to modify the database, and query the database to obtain the received data.

import pymysql
import threading
import time
from tkinter import  *

class MyThread1(threading.Thread):
    def __init__(self, d):
        super(MyThread1, self).__init__()  # 重构run函数必须要写
        self.userid = d[0]
        self.cursor=d[1]

    def run(self):
        time_start = time.time()
        ppid=-1
        while (1):
            time_end = time.time()
            if time_end - time_start > 1:
                time_start = time.time()
                db = refreash()
                self.cursor = db.cursor()  # 获取光标
                sql_select = "select receive from D where post=%s"
                self.cursor.execute(sql_select, self.userid)
                rett = self.cursor.fetchone()[0]
                rece=rett.strip()
                select_sql = "select * from D where post=%s"
                self.cursor.execute(select_sql, rece)
                receive = self.cursor.fetchone()
                postid=int(receive[0])
                data=receive[3]
                if data != None and postid!=ppid:
                    dat = data.strip()
                    ppid=postid
                    print(rece + ":" + dat)


class MyThread2(threading.Thread):
    def __init__(self, d):
        super(MyThread2, self).__init__()  # 重构run函数必须要写
        self.userid = d[0]
        self.cursor = d[1]

    def run(self):
        time_start = time.time()
        while (1):
            data = input("您输入的信息(输入0返回登录页):")
            if data == '0':
                menu(db)
            sql_query = "update D set data=%s,ID=ID+1 where post=%s"
            data = data.encode('GBK')
            self.cursor.execute(sql_query, (data, self.userid))
            db.commit()


def refreash():
    db = pymssql.connect(
        host='***.***.***.***',
        port=3306,
        user='root',
        password='******',
        database='soft',
        charset='utf8'
    )  # 服务器名,账户,密码,数据库名
    return db

def menu(db):
    cursor = db.cursor()  # 获取光标
    userid=input("您的账号(输入exit退出系统):")
    if userid=='exit':
        exit()
    password=input("您的密码:")
    sql_select = "select password from U where userid=%s"
    cursor.execute(sql_select, userid)
    ps = cursor.fetchone()[0]
    if password==ps:
        print("登录成功!!!")
    else:
        print("登录失败!!!")
        menu(db)
    t1 = MyThread1((userid,cursor))
    t2 = MyThread2((userid,cursor))
    t1.start()
    t2.start()


if __name__ == '__main__':
    db = refreash()
    menu(db)

Guess you like

Origin blog.csdn.net/HUANGliang_/article/details/124337289