whoere开发

设计规划

开发轻量版的whoere,具备以下功能

1.用户之间可以相互私信,显示在线人数及id

2.每个用户可以发布公告,并支持查询公告、删除公告

具体实施

1.用户之间的私信,实现此功能需要用到服务器,及所有的用户与服务端直接通

信,各用户之间间接通信,服务端与客户端的通信需要用到socket模块,以下是

python的实现过程:

#服务端实现
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 19999)) #监听客户端的socket请求
s.listen(5)  #允许的最大连接数为5
sock, addr = s.accept()  #与客户端连接

socket是网络连接节点,我们使用浏览器浏览网页时,便是先向该网页所在的服务

器上发送socket请求,而服务器一直在监听,这样两边通过各自的socket通过连接

#客户端实现
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((' . . . ', 19999)) #向此ip的服务器发送请求(端口号19999)

以上是建立连接的过程,服务端与客户端的通信通过send、recv方法实现即可

sock.send(b'Sorry, not found')
nam = sock.recv(1024

2.客户端各自执行,单线程已无法满足,这样用多线程来实现。服务端需要同时实

现与与客户端的连接、对客户端的监听,以下是实现过程

while True:
    sock, addr = s.accept()  #监听客户端的状态
    t = threading.Thread(target = link, args = (sock, addr))
    #link是服务端与客户端sock连接的过程
    t.start()

同时客户端也需要用一个新的线程来实现对服务器信息的监听

def check(x):
  while True:
    print(s.recv(1024).decode('utf-8'))
t = threading.Thread(target = check, args = (10, ))
t.start()

服务端程序:

import socket
import time
import threading
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 19999))
s.listen(5)
print('writing for connecting...')
global soc, user_id, tot
soc = []
user_id = []
tot = 0
t_name = []
topic = []
def comm_help(sock):
    sock.send(b'send:Send a message to sb')
    sock.send(b'add:Add a public announcement\n')
    sock.send(b'del:To delete a public announcement\n')
    sock.send(b'list:List all the announcement')
    sock.send(b'help:A command prompt\n')
    sock.send(b'who:List the number of online\n')
def comm_add_topic(peo, sock):
    global t_name, topic
    sock.send(b'What do you want to publish the announcement ?')
    topic_content = sock.recv(1024)
    topic = topic + [topic_content]
    t_name = t_name + [peo]
    sock.send(b'Operation is successful')
def comm_list_topic(sock):
    global topic, t_name
    for i in range(len(topic)):
       ans = str(i + 1) + '. ' + topic[i].decode('utf-8') + '------By ' + t_name[i].decode('utf-8')
       sock.send(ans.encode('utf-8'))
def comm_list_peo(sock):
    global user_id
    for i in range(len(user_id)):
       tt = str(i + 1) + '. ' + user_id[i].decode('utf-8') 
       sock.send(tt.encode('utf-8'))   
def comm_delete_topic(x, sock, peo):
    num = int(x) - 1
    global t_name, topic
    if (num < 0) or (num >= len(topic)):
        sock.send(b'Sorry, not found')
        return
    if peo != t_name[num]:
        sock.send(b'Sorry,you have no legal power operation')
        return
    del topic[num]
    del t_name[num]
    sock.send(b'Operation is successful')
def comm_s(name, sock):
    global soc, user_id
    sock.send(b'please enter your aim_id')
    iid = sock.recv(1024)
    sock.send(b'please enter your aim_thing')
    thing = sock.recv(1024)
    num = user_id.index(iid);
    print(num)
    dat = 'Received a new reply from ' + name.decode('utf-8') + ':'
    soc[num].send(dat.encode('utf-8'))
    soc[num].send(thing)
    for i in range(tot):
        print(str(i) + ' : ' +user_id[i].decode('utf-8'));
def link(sock, addr):
    global tot;
    tot = tot + 1;
    print('Accept new connection from %s:%s....'  % addr)
    sock.send(b'welcome,please enter your name')
   # sock.send(b'this is only for test')
    # t = 'the number of online people' + str(tot);
    # sock.send(t.encode('utf-8'))
    iid = sock.recv(1024)
    global soc, user_id
    soc = soc + [sock]
    user_id = user_id + [iid]
    while True:
        data = sock.recv(1024)
        time.sleep(1)
        if not data or data.decode('utf-8') == 'exit':
           break
        elif data.decode('utf-8') == 'send':
           nam = sock.recv(1024)
           comm_s(nam, sock)
        elif data.decode('utf-8') == 'list':
           comm_list_topic(sock)
        elif data.decode('utf-8') == 'add':
           nam = sock.recv(1024)
           comm_add_topic(nam, sock)
        elif data.decode('utf-8') == 'del':
           peo = sock.recv(1024)
           sock.send(b'Please enter the number you want to delete')
           nam = sock.recv(1024)
           comm_delete_topic(nam, sock, peo)
        elif data.decode('utf-8') == 'help':
           comm_help(sock)
        elif data.decode('utf-8') == 'help':
           comm_list_peo(sock)
        else:
           sock.send(b'This command is invalid')
         #sock.send(('Hello, %s' % data.decode('utf-8')).encode('utf-8'))
    sock.close()
    print('Connection from %s: %s closed' % addr)
while True:
    sock, addr = s.accept()
    t = threading.Thread(target = link, args = (sock, addr))
    t.start()

客户端程序:

import socket
import threading
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('23.105.193.235', 19999))
#----------------- listen -------------------------
def check(x):
  while True:
    print(s.recv(1024).decode('utf-8'))
t = threading.Thread(target = check, args = (10, ))
t.start()
#---------------------------------------------------
#print(s.recv(1024).decode('utf-8'))
iid = input()
s.send(iid.encode('utf-8'))
def comm_add():
  global iid, s
  s.send(iid.encode('utf-8'))
  data = input()
  s.send(data.encode('utf-8'))
def comm_send():
  global s, iid
  s.send(iid.encode('utf-8'))
  iid = input()
  s.send(iid.encode('utf-8'))
  thing = input()
  s.send(thing.encode('utf-8'))
def comm_del():
  global s, iid
  s.send(iid.encode('utf-8'))
  num = input()
  s.send(num.encode('utf-8'))
s.send(b'help')
while True:
    time.sleep(2)
    print('please enter all you want to do')
    data = input();
    tt ='Being performed---->' + data;
    print(tt)
    if data == 'exit':
       s.send(b'exit')
       break;
    s.send(data.encode('utf-8'))
    if data == 'send':
       comm_send()
    if data == 'add':
       comm_add()
    if data == 'del':
       comm_del()

s.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值