Κρίμα στην python 2.7 δεν υπάρχει αυτήν η μέθοδος shutdown.
Αν έχετε όρεξη να διαβάσετε όλο των κώδικα δεν έχω πρόβλημα.
# -*- coding: utf-8 -*-
import wx, threading, socket
running = True
title = "Private Comminicator"
pixels = (300,150)
myList = ['a', 'b', 'c']
IP = ""
PORT = 5456
sock = None
client = []
address = []
class guiApp(wx.Frame):
def __init__(self, parent, __id__):
wx.Frame.__init__(self, parent, __id__, title, size = pixels)
self.panel = wx.Panel(self)
createMenu(self)
return
def createServer(self, event):
global IP, PORT
if (sock == None):
IP = Input("Give an IP address:", "Create Connection")
PORT = Input("Give a port:", "Create Connection")
if (IP == None or PORT == None):
return
self.acceptClient = threading.Thread(target = AcceptClientThread)
self.acceptClient.start()
return
else:
box = wx.MessageDialog(None, "You already running a server.", "Connection Failed", wx.OK)
box.ShowModal()
box.Destroy()
return
def joinServer(self, event):
global IP, PORT, sock
if (sock == None):
IP = Input("Give an IP address:", "Create Connection")
PORT = Input("Give a port:", "Create Connection")
if (IP == None or PORT == None):
return
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
PORT = int(PORT)
sock.connect((IP, PORT))
pass
except:
box = wx.MessageDialog(None, "Failed to join connection.", "Connection Failed", wx.OK)
box.ShowModal()
box.Destroy()
sock = None
return
else:
box = wx.MessageDialog(None, "You already joined a server.", "Connection Failed", wx.OK)
box.ShowModal()
box.Destroy()
return
def stopConnection(self, event):
global sock, client, address, running
if (sock != None):
sock.close()
sock = None
client = []
address = []
self.acceptClient.shutdown()
return
else:
box = wx.MessageDialog(None, "There is no connection.", "ERROR", wx.OK)
box.ShowModal()
box.Destroy()
return
def createMenu(self):
statusBar = self.CreateStatusBar()
menuBar = wx.MenuBar()
File = wx.Menu()
createConn = File.Append(wx.NewId(), "Create Connection", "This will create a server.")
joinConn = File.Append(wx.NewId(), "Join Connection", "This will join a server.")
stopConn = File.Append(wx.NewId(), "Stop Connection", "This will destroy a server or disconnect from a server.")
menuBar.Append(File, "File")
self.Bind(wx.EVT_MENU, self.createServer, createConn)
self.Bind(wx.EVT_MENU, self.joinServer, joinConn)
self.Bind(wx.EVT_MENU, self.stopConnection, stopConn)
self.SetMenuBar(menuBar)
return
def Input(message, title):
box = wx.TextEntryDialog(None, message, title)
if (box.ShowModal() == wx.ID_OK):
return box.GetValue()
pass
def AcceptClientThread():
global client, address, sock, PORT
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
PORT = int(PORT)
sock.bind((IP, PORT))
pass
except:
box = wx.MessageDialog(None, "Failed to create connection.", "Connection Failed", wx.OK)
box.ShowModal()
box.Destroy()
sock = None
return
sock.listen(1)
while (running):
cl, addr = sock.accept()
client.append(cl)
address.append(addr)
print("A client has been connected!")
if (__name__ == "__main__"):
app = wx.PySimpleApp()
frame = guiApp(None, -1)
frame.Show()
app.MainLoop()