如何阻止python在键盘中断时关闭连接?

时间:2017-06-19 16:55:38

标签: python sockets

所以,我试图制作一个小程序,显示带有消息的消息框我发送它。它工作正常,但当我关闭连接时,服务器脚本退出,我收到此消息:

- (void)viewDidLoad
{
    [super viewDidLoad];

      [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(receiveMenuItemSelectedNotification:)
                                                     name:@"MenuItemSelected"
                                                   object:nil];
    }

    -(void) receiveMenuItemSelectedNotification:(NSNotification*)notification
    {
        if ([notification.name isEqualToString:@"MenuItemSelected"])
        {
            NSDictionary* userInfo = notification.userInfo;
            NSNumber* tab = (NSNumber*)userInfo[@"tab"];
            NSLog (@"Successfully received test notification! %i", tab.intValue);
            self.tabBarController.selectedIndex = tab.intValue;

        }
    }

- (void)viewDidUnload
{
    [super viewDidUnload];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MenuItemSelected" object:nil];

}

这是我的python代码:

Traceback (most recent call last):   File "server.py", line 9, in <module>
    conn, addr = s.accept()   File "C:\Python27\lib\socket.py", line 206, in accept
    sock, addr = self._sock.accept() KeyboardInterrupt

当我关闭我的客户端脚本时,如何停止关闭服务器脚本?

1 个答案:

答案 0 :(得分:1)

KeyboardInterrupt只是一个例外。你应该能够抓住它:

try:
    # Your loop
except KeyboardInterrupt:
    # Code that gets executed when ctrl-c is pressed