So i've been working on this code that i got online and it is a chat server code.
This is the code-

# import needed modules:

from socket import *		# get sockets, for well, sockets
import string				# string functions
import time					# for sleep(1) function

# define global variables

HOST = ''				# Symbolic name meaning the local host
PORT = 4000				# Arbitrary non-privileged server
endl = "\r\n"			# standard terminal line ending

userlist = []			# list of connected users
done = 0				# set to 1 to shut this down

kAskName = 0			# some constants used to flag
kWaitName = 1			#	the state of each user
kOK = 2

# class to store info about connected users

class User:
	def __init__(self): = ""
		self.addr = ""
		self.conn = None
		self.step = kAskName

	def Idle(self):	
		if self.step == kAskName: self.AskName()

	def AskName(self):
		self.conn.send("Name? ")
		self.step = kWaitName

	def HandleMsg(self, msg):
		print "Handling message: ",msg
		global userlist
		# if waiting for name, record it
		if self.step == kWaitName:
			# try to trap garb initiall sent by some telnets:
			if len(msg) < 2 or msg=="#": return
			print "Setting name to: ",msg = msg
			self.step = kOK
			self.conn.send("Hello, "
			broadcast(" has connected."+endl)

		# check for commands
		if msg == "quit":
			broadcast(" has quit.\n")

		# otherwise, broadcast msg
		broadcast(": "+msg+endl )

# routine to check for incoming connections

def pollNewConn():
		conn, addr = s.accept()
		return None
	print "Connection from", addr
	user = User();
	user.conn = conn
	user.addr = addr
	return user

# routine to broadcast a message to all connected users

def broadcast(msg):
	for u in userlist:


# set up the server

s = socket(AF_INET, SOCK_STREAM)
s.bind(HOST, PORT)
print "Waiting for connection(s)..."

# loop until done, handling connections and incoming messages

while not done:
	time.sleep(1)		# sleep to reduce processor usage
	u = pollNewConn()	# check for incoming connections
	if u:
		print len(userlist),"connection(s)"

	for u in userlist:	# check all connected users
			data = u.conn.recv(1024)
			data = filter(lambda x: x>=' ' and x<='z', data)
			data = string.strip(data)
			if data:
				print "From",,': ['+data+']'
				if data == "shutdown": done=1

for u in userlist:

Now my problem is that when i try to connect, there is a traceback error
(Traceback (most recent call last):
File "E:\Python Files\Chat", line 98, in <module>
s.bind(HOST, PORT)
File "<string>", line 1, in bind
TypeError: bind() takes exactly one argument (2 given))
I've tried to switch the code around to make it work, but nothing seems to be working. Could use some help?

Recommended Answers

AFAIK, `bind` accepts a tuple; just wrap those HOST and PORT in another pair of parentheses and you should be good to go.

Jump to Post

All 3 Replies

AFAIK, `bind` accepts a tuple; just wrap those HOST and PORT in another pair of parentheses and you should be good to go.

They work the same, they need the same data type.



Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.