|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
sockets in pythong
anyone have any code that uses sockets in python that you wouldn't mind me seeing ?
Eli |
|
#2
|
|||
|
|||
|
# Server program -- Echos to the screen
# what was sent by the client program from socket import * import threading def svrThread(HOST,PORT): s = socket(AF_INET, SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) print 'Recived', `data` if not data: break conn.send(data) conn.close() global HOST HOST = '' # Symbolic name meaning the local host global PORT PORT = 50007 # Arbitrary non-privileged port threading.Thread(target=svrThread,args=(HOST,PORT)).start() # Client program # Uses TKinter... type in the one line text box at the bottom # all the text goes to the window at the top. from socket import * import threading from Tkinter import * def msgThread(addr,port): s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) s.connect((addr,port)) def sendMessage(msg): s.send(msg) def onQuit(): s.send('quit') sendThis='quit' root.quit() def onEnter(msg): sendThis = entry.get() lastSent = "" entry.delete(0,END) if sendThis != 'quit': if sendThis != lastSent: s.send(sendThis) lastSent = sendThis data = s.recv(1024) msgs.insert(END,data+'\n') else: s.close() def setup(root): global msgs,entry msgs = Text(root,width=60,height=20) msgs.grid(row=0,column=0,columnspan=3) s = Scrollbar(root,orient=VERTICAL) s.config(command=msgs.yview) msgs.config(yscrollcommand=s.set) s.grid(row=0,column=3,sticky=N+S) entry = Entry(root) entry.grid(row=1,column=0,columnspan=2,sticky=W+E) entry.bind("<Return>",onEnter) entry.focus_set() b = Button(root,text='Quit',command=onQuit) b.grid(row=1,column=2) global s s = socket(AF_INET, SOCK_STREAM) HOST = '127.0.0.1' # The remote host PORT = 50007 # The same port as used by the server root = Tk() root.title=("Chatting") setup(root) threading.Thread(target=msgThread,args=(HOST,PORT)).start() root.mainloop() The above two programs are the start for a 'chat room' type interface. They are not complete as is - but will work - you'll only be talking to yourself the way this is setup. But it should help you understand how the sockets work. Start by running the top program - once it starts you won't see anything. Then run the second program. Have Fun, let me know if you have any questions. If you modify the code I would like to see the changes you make.... |
|
#3
|
||||
|
||||
|
|
|
#4
|
|||
|
|||
|
The following code is an addition to the previous client program.
I added it just for some fun. The addition includes the win32com.client The reason for this is such that before the text appears in the window at the top it will 'speak' the text. Again - it's just for fun... it's not dealing with sockets. # Client program # Uses TKinter... type in the one line text box at the bottom # all the text goes to the window at the top. # This version will allow you to "listen" to the chat. # The program will speak what is dispalyed. from socket import * import threading from Tkinter import * import sys from win32com.client import constants import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice") def msgThread(addr,port): s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) s.connect((addr,port)) def sendMessage(msg): s.send(msg) def onQuit(): s.send('quit') sendThis='quit' root.quit() def onEnter(msg): sendThis = entry.get() lastSent = "" entry.delete(0,END) if sendThis != 'quit': if sendThis != lastSent: s.send(sendThis) lastSent = sendThis data = s.recv(1024) try: speaker.Speak(data) except: if sys.exc_type is EOFError: sys.exit() msgs.insert(END,data+'\n') else: s.close() def setup(root): global msgs,entry msgs = Text(root,width=60,height=20) msgs.grid(row=0,column=0,columnspan=3) s = Scrollbar(root,orient=VERTICAL) s.config(command=msgs.yview) msgs.config(yscrollcommand=s.set) s.grid(row=0,column=3,sticky=N+S) entry = Entry(root) entry.grid(row=1,column=0,columnspan=2,sticky=W+E) entry.bind("<Return>",onEnter) entry.focus_set() b = Button(root,text='Quit',command=onQuit) b.grid(row=1,column=2) global s s = socket(AF_INET, SOCK_STREAM) HOST = '127.0.0.1' # The remote host PORT = 50007 # The same port as used by the server root = Tk() root.title=("Chatting") setup(root) threading.Thread(target=msgThread,args=(HOST,PORT)).start() root.mainloop() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > sockets in pythong |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|