Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #////////////////////////////////////////////////
- """
- Here I imported lots of different modules.
- """
- #/////////////////////////////////////////////////
- from Tkinter import * #Used to create the GUI
- from ScrolledText import ScrolledText #Used to create the Scrolled Text
- import socket #Used to create the server
- import sys #Used for exiting in case of issue
- from thread import * #Creates threading capabilities.
- #////////////////////////////////////////////////
- """
- This class (CHANGE NEEDED HERE) is used to define the socket.
- It creates the server, and has a send_cmd method
- which is called upon by the Tkinter class
- Application
- """
- #/////////////////////////////////////////////////
- def start_up():
- HOST = socket.gethostname()
- PORT = 2468
- print "HOST: %s" % (HOST)
- print "PORT: %s" % (PORT)
- print "\n"
- print "-------------------------"
- print "\n"
- global s
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- print "Socket Created"
- try:
- s.bind((HOST, PORT))
- except socket.error, msg:
- print "Bind failed. Error Code : " + str(msg[0]) + " Message " + str(msg[1])
- sys.exit()
- print "Socket Bind Complete"
- """
- So by specifying 10, it means that if 10
- connections are already waiting to be processed,
- then the 11th connection request shall be rejected.
- This will be more clear after checking socket_accept.
- """
- s.listen(10)
- print "Socket now listening"
- def accept():
- while True:
- #Wait to accept a connection - blocking call
- connection, addr = s.accept()
- #display client information (IP address)
- print 'Connected with ' + addr[0] + ':' + str(addr[1])
- #Start new thread takees 1st argument as a function name to be run, second
- #is the tuple of arguments to the function
- start_new_thread(clientthread ,(connection,))
- s.close()
- #Function for handling connections. This will be used to create threads
- def clientthread(connection):
- #Sending message to connected client
- #This only takes strings (words
- connection.send("Welcome to the server. Type something and hit enter\n")
- #loop so that function does not terminate and the thread does not end
- while True:
- #Receiving from client
- data = connection.recv(1024)
- if not data:
- break
- connection.sendall(data)
- print data
- connection.close()
- def send_msg(s):
- #Send some data to the remote server
- message = app.message.get("0.0", END)
- #set the whole string
- s.sendall(message)
- myHistory = open("history.txt", "a+")
- myHistory.write(message)
- def main():
- start_up()
- accept()
- #////////////////////////////////////////////////
- """
- This class is used to hold the GUI stuff.
- It uses Tkinter and multiple frames to accomplish this task.
- It calls on commands from the Socket_Server class
- """
- #/////////////////////////////////////////////////
- class GUI(Frame): #This class is also a Frame. It is the window where other frames are held.
- def __init__(self, master): #initialize the grid and widgets
- Frame.__init__(self,master) #
- self.pastFUN()
- self.messageFun()
- start_up() # calls start_up function
- def pastFUN(self):
- self.pastFrame = Frame(root, width = 100, height = 300)
- self.pastFrame.grid()
- self.pastText = ScrolledText(self.pastFrame, height = 30,
- width = 50,
- #This line (state = DISABLED) means that the user cannot edit this textbox
- state = DISABLED,
- #This line (relief = GROOVE, bd = 5) tells Tkinter to make a groove aroudn the box, and the width of the border
- #Is now 5
- relief = GROOVE, bd = 5)
- self.pastText.grid(row = 1)
- def messageFun(self):
- self.messageFrame = Frame(root, width = 100, height = 100)
- self.messageFrame.grid()
- self.space = Label(self.messageFrame)
- self.space.grid()
- self.message = Text(self.messageFrame, height = 10, width = 50,
- relief = GROOVE, borderwidth = 5,)
- self.message.grid()
- self.send = Button (self.messageFrame, text = "Send",
- command = self.class_send_msg)
- self.send.grid(row = 1, column = 1, sticky = S)
- def class_send_msg(self):
- send_msg(s)
- #The functions that you find from now on are commands for Buttons
- root = Tk()
- root.title("MYIM Server GUI")
- #make my screen dimensions work
- w = 500
- h = 1000
- root.geometry("500x1000")
- app = GUI(root)
- root.mainloop()
Advertisement
RAW Paste Data
Copied
Advertisement