python實現(xiàn)點對點聊天程序
用Python實現(xiàn)點對點的聊天,2個程序,一個是client.py,一個是server.py,通過本機(jī)地址127.0.0.1連接進(jìn)行通信,利用多線程把發(fā)送消息和接收消息分開獨立進(jìn)行。
client代碼:
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代碼:
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個程序都是用函數(shù)的方法,還有一種方法是用類繼承threading類
代碼:
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()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PyCharm中New Directory 和 New Python
python package這是一個特殊的目錄,因為在創(chuàng)建該python package的時候,系統(tǒng)會自動地生成一個py文件, init.py,這篇文章主要介紹了PyCharm中New Directory 和 New Python Package的區(qū)別,需要的朋友可以參考下2023-12-12
python實現(xiàn)在每個獨立進(jìn)程中運行一個函數(shù)的方法
這篇文章主要介紹了python實現(xiàn)在每個獨立進(jìn)程中運行一個函數(shù)的方法,涉及Python操作進(jìn)程的相關(guān)技巧,需要的朋友可以參考下2015-04-04
使用Python創(chuàng)建快捷方式管理應(yīng)用
在Windows系統(tǒng)中,快速訪問常用程序通常通過“開始菜單”中的“應(yīng)用熱門”功能實現(xiàn),在這篇博客中,我將向你展示如何使用Python和wxPython創(chuàng)建一個GUI應(yīng)用,幫助用戶輕松將桌面上的快捷方式添加到Windows“開始菜單”的“應(yīng)用熱門”中,需要的朋友可以參考下2024-08-08
python中pytest收集用例規(guī)則與運行指定用例詳解
這篇文章主要介紹了python中pytest收集用例規(guī)則與運行指定用例詳解,天會講解一下pytest是如何收集我們寫好的用例?我們又有哪些方式來運行單個用例或者批量運行用例呢,需要的朋友可以參考下2019-06-06

