-1

I searched for answers relevant to this, but didn't come across any. I am writing a socket program for a P2P network for which I need to incorporate some signal handling.

If I press CTRL+C while I am running the program, the program should send a "LEAVE" request to another peer.

Pseudo code:

if conditionX
    dosomething(IP, port)
if conditionY
    dosomethingelse(IP, port)
if SIGINT detected
    sendleaverequest(peerip, port)

How can I implement signal handling here?

hax
  • 282
  • 1
  • 17

1 Answers1

2

CTRL-C is the one of the signal for operating system. You can handle signals in programming languages including signal libraries or modules.

For Python you can use system module, If you handle signal.SIGINT, you can execute your own code when you press CTRL-C.

Example Usage from the previous questions:

Visit: How do I capture SIGINT in Python?

Bilgehan
  • 117
  • 4