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)文章
python如何通過FastAPI構(gòu)建復(fù)雜的Web?API
FastAPI是一個現(xiàn)代的、快速(高性能)的Web框架,用于構(gòu)建API,這篇文章主要介紹了python如何通過FastAPI構(gòu)建復(fù)雜的Web?API,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02
基于python,Matplotlib繪制函數(shù)的等高線與三維圖像
這篇文章主要介紹了基于python,Matplotlib繪制函數(shù)的等高線與三維圖像,函數(shù)的等高線及其三維圖像的可視化方法,下面一起來學(xué)習(xí)具體內(nèi)容吧,需要的小伙伴可以參考一下2022-01-01
pandas read_excel()和to_excel()函數(shù)解析
這篇文章主要介紹了pandas read_excel()和to_excel()函數(shù)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Django model.py表單設(shè)置默認(rèn)值允許為空的操作
這篇文章主要介紹了Django model.py表單設(shè)置默認(rèn)值允許為空的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
詳解pycharm的python包opencv(cv2)無代碼提示問題的解決
這篇文章主要介紹了詳解pycharm的python包opencv(cv2)無代碼提示問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python使用wxPython和PyMuPDF提取PDF頁面指定頁數(shù)的內(nèi)容
在本篇博客中,我們將探討如何使用wxPython和PyMuPDF庫創(chuàng)建一個簡單的Bokeh應(yīng)用程序,用于選擇PDF文件并提取指定頁面的內(nèi)容,并將提取的內(nèi)容顯示在文本框中,需要的朋友可以參考下2023-08-08
Python RabbitMQ實現(xiàn)簡單的進(jìn)程間通信示例
這篇文章主要介紹了Python RabbitMQ實現(xiàn)簡單的進(jìn)程間通信示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
關(guān)于Tensorflow中的tf.train.batch函數(shù)的使用
本篇文章主要介紹了關(guān)于Tensorflow中的tf.train.batch函數(shù)的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

