Newbie.......Python and Networking

phil phillip.watts at anvilcom.com
Thu May 5 11:58:11 EDT 2005


xeron wrote:

> Thanks phil, I think I'll start off with your chat prog (UDP-- 50 lines
> code ) and then try for routing and firewall scripts for linux, yepp
> that would be really tough but why not give it a try, i am going to
> have my vacations for like 13 days so will be preparing for my CCNA and
> also learning python in that duration.
> 
> You have been most helpful through ur well thought and scaled reply
> 
> xeron
> 
> 
I could send you the chat program they wrote if it would help.

It has 2 threads, listener and sender.
Might spoil your fun.
However it is very primitive and asynchronous and needs work.

Oh heck, here it is.
DO NOT PEEK BEYOND THIS POINT IF YOU WISH TO SOLVE YOURSELF.



# chat1.py
# remote unknown, firewall allow UDP 12101

import os
from threading import *
from socket import *

HOST = ''
BUFSIZE = 1024
ListenPort = 12101
ListenAddr = (HOST,ListenPort)

listenersocket = socket(AF_INET,SOCK_DGRAM)
listenersocket.bind(ListenAddr)
sendsocket = socket(AF_INET,SOCK_DGRAM)

sendsocket = socket(AF_INET,SOCK_DGRAM)

def listener():
     while 1:
	msg,addr = listenersocket.recvfrom(BUFSIZE)
	print '<',msg
	if msg == 'die-now': break

def sender():
     print; print
     print 'Type away.  When you are thru with your'
     print '  message, hit enter an extra time'
     print '  to let the other guy know its his turn.'
     print 'To exit, enter exit'
     print; print

     while 1:
	msg = raw_input()
	if msg == 'exit':
                 sendsocket.sendto( 'die-now', ('localhost',ListenPort) )
		break
         sendsocket.sendto( msg, (sendtoip,ListenPort) )
	
# Main program start here
print
sendtoip = raw_input('Enter IP address of other computer: ')
sendtoip = sendtoip.strip()

# Define and start 2 threads
listnr = Thread(target=listener)
sendr = Thread(target=sender)
listnr.start()
sendr.start()
listnr.join()
sendr.join()








More information about the Python-list mailing list