Python的socket多线程编程实现(附可实现源码)

该博客介绍了一个使用Python实现的多线程socket服务器,能够通过字典集进行简单的智能交流。服务器监听客户端连接,接收消息并使用线程发送回复。客户端则持续发送用户输入的数据直到输入'exit'退出。博主提到了尝试使用开源第三方库实现更智能的聊天功能,但目前无法免费测试。源代码可供学习,不应用于商业目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mySocket

Introduction

Using python language to complete socket multi-threaded programming and pseudo-intelligent communication through dictionary set.

The following shows part of the source code.

Server.py

import socket
import threading
import time
import os

#字典集返回数据
def getAnswer(c_data):
    words = {'how are you?': 'Fine,thank you.',  # 字典集
             'hi': 'hello',
             'hello': 'hello',
             'how old are you?': '20',
             'what is your name?': 'Andel,
             "what's your name?": 'Andel',
             'where do you work?': 'Beijing',
             'bye': 'Bye'
             }
    flag = 0
    lst = list(words.keys())
    for i in range(len(lst)):
        if len(os.path.commonprefix([c_data, lst[i]])) > len(lst[i]) * 0.8:
            flag = 1
            return words.get(lst[i])
    if flag == 0:
        return "Sorry for that, I don
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值