脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python实现点对点聊天程序

python实现点对点聊天程序

2021-03-23 00:28csuzhucong Python

这篇文章主要为大家详细介绍了python实现点对点聊天程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

用Python实现点对点的聊天,2个程序,一个是client.py,一个是server.py,通过本机地址127.0.0.1连接进行通信,利用多线程把发送消息和接收消息分开独立进行。

client代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import socket
import sys
import threading
import time
 
class client():
 def __init__(self):
  self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  self.ip = "127.0.0.1"
 
 def connect(self):
  try:
   self.s.connect((self.ip,8888))
   print("connect success")
   print('connect time: '+time.ctime())
  except ConnectionError:
   print('connect error')
   sys.exit(-1)
  except:
   print('unexpect error')
   sys.exit(-1)
 
 def send_sth(self):
  while True:
   sth=input('say something:\n')
   try:
    self.s.sendall(sth.encode('utf-8'))
   except ConnectionError:
    print('connect error')
    sys.exit(-1)
   except:
    print('unexpect error')
    sys.exit(-1)
 
 def receive(self):
  while True:
   try:
    r=self.s.recv(1024)
    print ('get message:'+r.decode('utf-8'))
   except ConnectionError:
    print('connect error')
    sys.exit(-1)
   except:
    print('unexpect error')
    sys.exit(-1)
 
c1 = client()
c1.connect()
threading._start_new_thread(c1.receive,())
c1.send_sth()

server代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import socket
import sys
import threading
import time
 
def server():
 def bind():
  HOST='127.0.0.1'
  s.bind((HOST,8888))
  print("bind ok")
 
 def listen():
  s.listen(10)
  print ('Socket now listening')
 
 def send_sth(conn):
  while True:
   try:
    sth=input('say something:\n')
    conn.sendall(sth.encode('utf-8'))
   except ConnectionError:
    print('connect error')
    sys.exit(-1)
   except:
    print('unexpect error')
    sys.exit(-1)
 
 def recv(conn):
   while True:
   try:
    data=conn.recv(1024)
    data2=data.decode('utf-8')
    print('get message:'+data2)
   except ConnectionError:
    print('connect error')
    sys.exit(-1)
   except:
    print('unexpect error')
    sys.exit(-1)
 
 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 bind()
 listen()
 conn,addr=s.accept()
 print("connect success")
 print('connect time: '+time.ctime())
 threading._start_new_thread(recv,(conn,))
 send_sth(conn)
 
if __name__=='__main__':
 server()

开启多线程有2种方法,上面2个程序都是用函数的方法,还有一种方法是用类继承threading类

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import socket
import threading
class client(threading.Thread):
 def __init__(self,sth):
  threading.Thread.__init__(self)
  self.sth=sth
 def run(self):
  s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  ip="127.0.0.1"
  try:
   s.connect((ip,8888))
  except :
   print('con error')
   exit()
  #print("connect success")
  s.sendall(self.sth.encode('utf-8'))
  #print("send success")
  try:
   r=s.recv(1024)
  except:
   print('recv error')
   exit()
  print (r.decode('utf-8'))
c1=client('hello 1')
c1.start()
c2=client('hello 2')
c2.start()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/nameofcsdn/article/details/75119572

延伸 · 阅读

精彩推荐