色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

如何終止用python寫的socket服務(wù)端程序?

瀏覽:91日期:2022-09-18 08:40:59

問題描述

用python寫了一個socket服務(wù)端的程序,但是啟動之后由于監(jiān)聽連接的是一個死循環(huán),所以不知道怎樣在cmd運行程序的時候?qū)⑵浣K止。

#!/usr/bin/python# -*- coding: utf-8 -*-import socketimport threading, timedef tcplink(sock,addr): print(’Accept new connection from %s:%s...’ %addr) sock.send(b’Welcome!’) while True:data=sock.recv(1024)time.sleep(1)if not data or data.decode(’utf-8’)==’exit’: breaksock.send((’Hello,%s!’% data.decode(’utf-8’)).encode(’utf-8’)) sock.close() print(’Connection from %s:%s closed.’ % addr) s=socket.socket()s.bind((’127.0.0.1’,1234))s.listen(5)print(’Waiting for conection...’)while True: #accept a new connection sock,addr=s.accept() #create a new thread t=threading.Thread(target=tcplink,args=(sock,addr)) t.start()

在win10上的cmd運行后的情況是按ctrl+c,ctrl+z,ctrl+d都不能終止,請問要怎么終止程序?

問題解答

回答1:

Ctrl + C

回答2:

在啟動線程之前,添加 setDaemon(True)

while True: #accept a new connection sock,addr=s.accept() #create a new thread t=threading.Thread(target=tcplink,args=(sock,addr)) t.setDaemon(True) # <-- add this t.start()

daemon

A boolean value indicating whether this thread is a daemonthread (True) or not (False). This must be set before start() iscalled, otherwise RuntimeError is raised. Its initial value isinherited from the creating thread; the main thread is not a daemonthread and therefore all threads created in the main thread default todaemon = False.

The entire Python program exits when no alive non-daemon threads areleft.

這樣 <C-c> 的中斷信號就會被 rasie。

回答3:

kill -9回答4:

關(guān)閉cmd命令窗口,重新開啟一個cmd,我是這么做的。

回答5:

可以使用signal模塊,當(dāng)按住Ctrl+C時,捕捉信息,然后退出.

#!/usr/bin/env python# -*- coding: utf-8 -*-import signaldef do_exit(signum, frame): print('exit ...') exit()signal.signal(signal.SIGINT, do_exit)while True: print('processing ...')回答6:

我記得可以

try: ......except KeyboardInterrupt: exit()

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 亚洲福利精品一区二区三区 | 手机在线观看毛片 | 久久免费成人 | 成人国产精品视频 | 国产三级精品91三级在专区 | 成人精品国产 | 高清不卡一区二区三区 | 免费人成在线观看 | 亚洲在线免费观看视频 | 男女男精品视频在线播放 | 亚洲国产www | 欧美一级特黄特黄做受 | 丝袜紧身裙国产在线播放 | 国产片网址 | 一区高清 | 亚洲国产精品成人午夜在线观看 | 欧美一级成人毛片影院 | 国产精品午夜性视频网站 | 亚洲国语| 精品91一区二区三区 | 国产精品日韩 | 91小视频在线观看免费版高清 | 久久手机精品视频 | a级午夜毛片免费一区二区 a级性生活视频 | 完整日本特级毛片 | 亚洲成a人不卡在线观看 | 国产视频99 | 91精品免费国产高清在线 | 在线观看免费国产成人软件 | 亚洲一区二区三区中文字幕 | 国产免费爱在线观看视频 | 亚洲欧美综合国产精品一区 | 精品国产日韩亚洲一区在线 | 国产亚洲精品一区二区在线播放 | 国产一区亚洲 | 国产三级做爰在线观看视频 | 在线精品欧美日韩 | 国产在线综合一区二区三区 | a级毛片在线看日本 | 成人久久18免费软件 | 性日韩精品 |