Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PyChat - Python Code Bank


PyChat
Simple turn-based chatting module to be expounded upon.
                #PyChat coded by rootDaemon
#----------------------------
#Allows two computers to communicate in a turn taking fashion
#Requires internet connection and IP address
#Ip can be found by CMD "ipconfig /all"
#Python.exe must be allowed thru firewall
#---------------------------

import socket

#--------Functions----------

def greet():                          #startup message, displayed only once
	print 'Welcome to PyChat...'  


#obtain port if not using default
#Client and Server port number must match
def getPort():                          #port must be non-priveledged
	dport = raw_input('Please choose a port : ') 
	return int(dport)



#obtain Host IP...
def getHOST():
	dhost = raw_input("Enter Host's IP address : ")
	return dhost



#find out broadcast name for transmisson and verification on server side
def getName():
	anon = raw_input('Broadcast a client connection title ? (y/n) : ')
	if anon == 'y':                 #if use broadcast name
		bName = raw_input('title = ')   #assign
	else:                           #otherwise broadcast anonymous
		bName = '0'

	return bName

#Set up and host a connection on server side
def serverChannel():
	defaultP = raw_input('Host on default port OK ? (y/n) : ')
	HOST = ''                        # all available interfaces
	if defaultP == 'y':
		PORT = 60001             # Arbitrary non-privileged port
	else:
		PORT = getPort() ;
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
	s.bind((HOST, PORT))
	s.listen(1)                      #wait for client
	print 'waiting for connection...'
	conn, addr = s.accept()
	print '==='
	print 'Connected by', addr
	data = conn.recv(1040)          #get title broadcast by client
	if data == '0' :                #if title is '0', no title broadcasted
		print 'The client has not broadcasted a title. Connect to anonymous?'
	else:
		print 'Accept connection to ', data, '?'
	i = 1
	n = raw_input('(y/n) : ')
	if n == 'n':                  #server denies connection
		conn.send('\c')         #send close message to client
		conn.close()            #close connection
		i = 0
	else:
		conn.send('o')          #arbitrary value except '\c'
	if i:
		idata = ''
		print '\n'
		print '\c to close'
		print 'waiting for client...'
		print '\n\n\n'
		while idata != '\c':          #while not close message
			data = conn.recv(1024)  #get data
			if data == '\c':        #if client closes
				print 'Connection closed by client'
				return
			print '   >>> ', data   #otherwise print client data
			idata = raw_input('$')
			conn.send(idata)
		conn.send('host has closed connection') #if input == \c
		conn.close()

def clientChannel():
	defaultP = raw_input('Connect on default port OK ? (y/n) : ')
	
	if defaultP == 'y':
		PORT = 60001                #Arbitrary non-privileged port
	else:
		PORT = getPort() ;

	HOST = getHOST()                  #Get Host IP from function
	
	title = getName()                 #Get broadcast title
	
	conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	print 'attempting a connection...'
	try:                              #Attempt to connect
		conn.connect((HOST,PORT))
	except:                           #If connection fails
		print 'Connection could not be established with Host ', HOST, ' on port ', PORT
		return
	conn.send(title)                  #Send server broadcast title
	i = 1
	data = conn.recv(1024)            #Check if connection closed
	if data == '\c':                  #If close message
		print 'connection was denied by server...'
		i = 0
	else:                             #Otherwise...
		print 'connection accepted'
	if i:
		idata = ''
		print '\n'
		print '\c to close'
		print 'You have input...'
		print '\n\n\n'
		while idata != '\c':
			idata = raw_input('$')
			conn.send(idata)
			if idata == '\c':       #check server input for close message
				break
			data = conn.recv(1024)
			if data == '\c':        #check client input for close message
				print 'Host has closed connection'
				return
			print '   >>> ', data
		conn.close()


# Main Module
#------------------------------------------

greet()

again = 'y'
while again == 'y':                   #countinue running until user quits (again != 'y')
	conType = raw_input('Be server or client? (s/c) : ')
	if conType == 's':              #if server
		serverChannel()           #run server code
	else :
		if conType == 'c':        #if client
			clientChannel()     #run client code
		else:                     #Otherwise...
			print conType, ' is not a valid input'
	if conType == 's':
		again = raw_input('host another connection ? (y/n) : ')
	else:
		again = raw_input('attempt another connection ? (y/n) : ')

            
Comments
Sorry but there are no comments to display