资源简介
利用socket套接字函数来实现一个聊天系统:
1.新用户注册
2.用户登录和退出
3.聊天服务
4.在线用户查询
5.文件传输服务
代码片段和文件信息
import socket
import argparse
import re
import time
import os
import sys
import threading
import getpass
import hashlib
User = None
def get_md5_value(s):
my_md5 = hashlib.md5()
my_md5.update(s.encode(‘utf-8‘))
md5_value = my_md5.hexdigest()
return md5_value
def register(sock):
while True:
while True:
UserName = input (‘Please input your username(4-16 numbers or letters):\n‘)
if len(UserName) < 4:
print(‘Your name is too short!‘)
continue
elif len(UserName) > 16:
print(‘Your name is too long!‘)
continue
elif re.match(r‘[a-zA-Z0-9]{416}$‘UserName) == None:
print(‘Username is illegal!‘)
continue
else:
break
while True:
PassWord = input (‘Please input your password(8-24 numbers or letters)\n‘)
if len(PassWord) < 8:
print(‘Your password is too short!‘)
continue
elif len(PassWord) > 24:
print(‘Your password is too long!‘)
continue
elif re.match(r‘[a-zA-Z0-9]{824}$‘PassWord) == None:
print(‘Password is illegal!‘)
continue
else:
PassWord1 = input(‘Input your password again:\n‘)
if PassWord1 != PassWord:
continue
else:
break
PassWord = get_md5_value(PassWord)
message = bytes(UserNameencoding = ‘utf-8‘) + b‘‘ + bytes(PassWordencoding = ‘utf-8‘)
sock.sendall(message)
serverback = sock.recv(1024).decode(‘utf-8‘)
if serverback == ‘0‘:
print(‘Username already exists‘)
continue
else:
print(‘Register successfully!‘)
time.sleep(1)
os.system(‘cls‘)
break
def log_in(sock):
global User
while True:
UserName = input (‘Please input your username:\n‘)
PassWord = getpass.getpass(‘Please input your password:\n‘)
PassWord = get_md5_value(PassWord)
message = bytes(UserNameencoding = ‘utf-8‘) + b‘‘ + bytes(PassWordencoding = ‘utf-8‘)
User = UserName
sock.sendall(message)
serverback = sock.recv(1024).decode(‘utf-8‘)
if serverback == ‘0‘:
print(‘Incorrect username or passwordplease re-enter!‘)
continue
else:
print(‘Log in successfully!‘)
time.sleep(1)
os.system(‘cls‘)
break
if not os.path.exists(‘e:\\file_received\\‘+UserName):
os.makedirs(‘e:\\file_received\\‘+UserName)
def log_out(sock):
serverback = sock.recv(1024).decode(‘utf-8‘)
if serverback == ‘1‘:
print(‘Log out successfully!‘)
else:
print(‘Log out error!‘)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 8321 2018-12-05 00:27 server.py
文件 9028 2018-11-30 14:16 client.py
- 上一篇:sm4国密算法python实现
- 下一篇:Python爬取b站视频弹幕并可视化案例
相关资源
- Python爬取b站视频弹幕并可视化案例
- sm4国密算法python实现
- python面试题100道
- python_docx-0.8.10-py2.py3-none-any.whl
- python3.4中文学习手册chm
- python 2.7中文手册chm版
- 产生式系统包含动物识别规则库,手
- 数据挖掘教学视频实战超详细加pyth
- python实现KNN手写字体识别Demo
- python q-learning
- python3网络爬虫与开发实战崔庆才PDF百
- python语言实现通过树莓派Pi camera的图
- 扫雷 python
- python习题
- ngramScore适应度计算文件(包含引入的
- 爱鲜蜂项目源码
- python300G视频书籍教程.zip
- Python点餐案例
- apriori python 3
- Python-Django搭建简单网站源码
- PSO优化的BP神经网络——python实现
- 基于Python专业网络爬虫的设计与实现
- 爬虫视频案例课程----崔庆财
- 漏洞验证脚本ssl-deacth-alert(cve-2016-
- 后台管理系统 v1.0.0
- python获取城市各公交线路及公交站点
- 麦子学院Python视频.txt
- starfm算法实现
- PYQT5+图片拖拽
- python+sqlite学生成绩管理
评论
共有 条评论