python 点对点聊天_python实现简单聊天应用 python群聊和点对点均实现

后续代码更新和功能添加会提交到个人github主页,有兴趣可以一起来完善!

如果只是拿过去运行看结果,请注意平台相关性以及python版本号,本示例开发运行平台为win7x86_64 pycharm community,python版本号为3.5!!!

TALK IS CHEAP, SHOW YOU MY CODE:

客户端

#coding:utf-8

'''

file:client.py.py

date:2017/9/11 11:01

author:lockey

email:lockey@123.com

platform:win7.x86_64 pycharm python3

desc:p2p communication clientside

'''

from socket import *

import threading,sys,json,re

#引入json模块主要是为了数据的封装传输,re的话是做一些合法性的验证

HOST = '192.168.1.7'

PORT=8022

BUFSIZE = 1024 ##缓冲区大小 1K

ADDR = (HOST,PORT)

myre = r"^[_a-zA-Z]\w{0,}"

tcpCliSock = socket(AF_INET,SOCK_STREAM)

#创建一个socket连接

userAccount = None

#用户登录标志,也用来记录登录的用户名称

def register():

#用户注册函数

print("""

Glad to have you a member of us!

""")

accout = input('Please input your account: ')

if not re.findall(myre, accout):

print('Account illegal!')

return None

password1 = input('Please input your password: ')

password2 = input('Please confirm your password: ')

if not (password1 and password1 == password2):

print('Password not illegal!')

return None

global userAccount

userAccount = accout

regInfo = [accout,password1,'register']

datastr = json.dumps(regInfo)

tcpCliSock.send(datastr.encode('utf-8'))

data = tcpCliSock.recv(BUFSIZE)

data = data.decode('utf-8')

if data == '0':

print('Success to register!')

return True

elif data == '1':

print('Failed to register, account existed!')

return False

else:

print('Failed for exceptions!')

return False

def login():

#用户登录函数

print("""

Welcome to login in!

""")

accout = input('Account: ')

if not re.findall(myre, accout):

print('Account illegal!')

return None

password = input('Password: ')

if not password:

print('Password illegal!')

return None

global userAccount

us

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值