国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

如何終止用python寫的socket服務端程序?

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

問題描述

用python寫了一個socket服務端的程序,但是啟動之后由于監聽連接的是一個死循環,所以不知道怎樣在cmd運行程序的時候將其終止。

#!/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:

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

回答5:

可以使用signal模塊,當按住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()

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 女人张开腿让男人桶免费网站 | 国产成人盗拍精品免费视频 | 99热久久精品免费精品 | 亚洲天堂色网站 | 亚洲逼| 欧美日本一区二区三区道 | a级毛片免费完整视频 | 久草不卡视频 | 久久久久亚洲视频 | 337p欧美| 亚洲精品成人一区二区www | 黄 色 三 片 | 欧美国产一区二区三区 | 亚洲高清在线观看看片 | 国产免费视屏 | 亚洲精品国自产拍在线观看 | 国产成人久久精品激情91 | 成人精品视频网站 | 国产香蕉影视院 | 日本免费特黄aa毛片 | 国产精品福利午夜h视频 | 亚洲视频手机在线观看 | 狠狠综合久久久久综合 | 欧美一级www| 视频一区亚洲 | 97在线视频精品 | 亚洲男女免费视频 | 国产成人精品亚洲日本在线观看 | 高清一级淫片a级中文字幕 高清一区二区 | 久久久久久久国产精品毛片 | 九九热视频精品 | 国产成人免费观看 | 日韩日b视频 | 91chinesevideo在线观看 | 亚洲精品国产成人7777 | 国产全部理论片线观看 | 国产三级做爰高清视频a | 久久久黄色片 | 欧美精品亚洲 | 99精品免费观看 | 欧美日韩 国产区 在线观看 |