Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 17th, 2003, 04:21 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
sockets in pythong

anyone have any code that uses sockets in python that you wouldn't mind me seeing ?

Eli

Reply With Quote
  #2  
Old October 17th, 2003, 08:16 AM
irishtek irishtek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Tucson AZ
Posts: 29 irishtek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to irishtek Send a message via AIM to irishtek Send a message via Yahoo to irishtek
# 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....

Reply With Quote
  #3  
Old October 17th, 2003, 09:54 AM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear

Reply With Quote
  #4  
Old October 17th, 2003, 10:25 AM
irishtek irishtek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Tucson AZ
Posts: 29 irishtek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to irishtek Send a message via AIM to irishtek Send a message via Yahoo to irishtek
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()

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > sockets in pythong


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway