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
- """
- #/////////////////////////////////////////////////
- BUFFER = 1024
- def start_up():
- HOST = socket.gethostname()
- IP = socket.gethostbyname(HOST)
- PORT = 2468
- print "HOST: %s" % (HOST)
- print "HOST IP: %s" % (IP)
- 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 mainloop(s):
- while True:
- channel, addr = s.accept()
- print "Connected with", addr
- def RECEIVE(s):
- try:
- #Sending message to connected client
- #This only takes strings (words
- s.recv(BUFFER)
- except Exception:
- import traceback
- print traceback.format_exc()
- def send_msg(s,message):
- #Send some data to the remote server
- my_message = message.get("0.0", END)
- #set the whole string
- s.sendall(my_message)
- myHistory = open("history.txt", "a+")
- myHistory.write(my_message)
- def main():
- start_up()
- mainloop(s)
- main()
- #////////////////////////////////////////////////
- """
- 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
- """
- #/////////////////////////////////////////////////
- #Setup Stuff
- root = Tk()
- root.title("HoweIM Server GUI")
- #make my screen dimensions work
- w = 500
- h = 1000
- root.geometry("500x1000")
- #Create the Main-Frame
- Hold_Frame = Frame(root)
- Hold_Frame.grid()
- #Past Frame
- pastFrame = Frame(Hold_Frame, width = 100, height = 300)
- pastFrame.grid()
- pastText = ScrolledText(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)
- pastText.grid(row = 1)
- #Message Frame
- messageFrame = Frame(Hold_Frame, width = 100, height = 100)
- messageFrame.grid()
- space = Label(messageFrame)
- space.grid()
- message = Text(messageFrame, height = 10, width = 50,
- relief = GROOVE, borderwidth = 5,)
- message.grid()
- send = Button (messageFrame, text = "Send",
- command = lambda: send_msg(s,message))
- send.grid(row = 1, column = 1, sticky = S)
- #Side Frame
- sideFrame = Frame(Hold_Frame)
- root.mainloop()
- #The functions that you find from now on are commands for Buttons
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement