亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

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

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

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

問題描述

用python寫了一個(gè)socket服務(wù)端的程序,但是啟動(dòng)之后由于監(jiān)聽連接的是一個(gè)死循環(huán),所以不知道怎樣在cmd運(yùn)行程序的時(shí)候?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運(yùn)行后的情況是按ctrl+c,ctrl+z,ctrl+d都不能終止,請(qǐng)問要怎么終止程序?

問題解答

回答1:

Ctrl + C

回答2:

在啟動(dòng)線程之前,添加 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> 的中斷信號(hào)就會(huì)被 rasie。

回答3:

kill -9回答4:

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

回答5:

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

#!/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)文章:
主站蜘蛛池模板: 九九视频国产 | 毛片免费观看成人 | 草草影院www色欧美极品 | 伊人热久久 | 欧美一区二区三区免费不卡 | 国产成人www免费人成看片 | 久草 在线| 91福利国产在线观看香蕉 | 日本69色视频在线观看 | 欧美人成在线观看网站高清 | 欧美在线视频免费 | 亚洲国产一区二区三区a毛片 | 欧美在线一级毛片观看 | 亚洲六月丁香六月婷婷蜜芽 | 久久国产亚洲 | 一级风流片a级国产 | 国产免费久久精品99re丫y | 久久久久久免费一区二区三区 | 美国全免费特一级毛片 | 国产精品福利午夜h视频 | 国产一区二区三区不卡在线观看 | 美女又黄又免费 | 欧美色道 | 黄色成人免费观看 | 国产男女猛烈无遮档免费视频网站 | 久久久久女人精品毛片 | 精品久久久久久久久久香蕉 | 久久福利资源国产精品999 | 欧美成人四级hd版 | 国内精品久久久久影院不卡 | 国产浮力第一页草草影院 | 国产v片成人影院在线观看 国产v片在线播放免费观 | 国产精品久草 | 亚洲精品中文一区不卡 | 国产伦码精品一区二区三区 | 欧美一级在线看 | 精品国产综合区久久久久99 | 成人毛片免费免费 | 欧美.亚洲.日本一区二区三区 | 毛片免费网址 | 日本一级在线播放线观看免 |