<commit_before>import socket, select

sockets = [] 
port = 8001

names = {}

def router():
	server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	server_socket.bind(("", port))
	server_socket.listen(10)
	
	sockets.append(server_socket)
	print("Server started on port %s" % port)

	while True:
		# check if there are any changes
		ready_to_read,ready_to_write,in_error = select.select(sockets,[],[],0)

		for sock in ready_to_read:
			if sock == server_socket: 
				new_sock, addr = server_socket.accept()
				sockets.append(new_sock)
				print("Client (%s, %s) connected" % addr)
			else:
				try:
					# get messages from the client
					data = sock.recv(4096)
					if data:
						data = str(data)
						# there is something in the socket
						if data[:5] == "/name":
							names[data[6:]] = sock
						else:
							send(sock, data)  
					else:
						# remove the socket that's broken    
						if sock in sockets:
							sockets.remove(sock)

						# at this stage, no data means probably the connection has been broken
						#broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr) 
				except:
					broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
					continue

	server_socket.close()

def send(sender, data):
	# find sender name
	sender_name = "OOPS"
	for key, val in names.values():
		if val == sender:
			sender_name = key
	sep = data.find(":")
	receiver = data[:sep]
	msg = data[sep+1:].strip()
	if receiver in names:
		names[receiver].send("%s: %s" % (sender_name, data))
	else:
		sender.send("Could not find user %s" % receiver)
	
if __name__ == "__main__":
	router()<commit_msg>added encode decode for python3 byte strings vs unicode strings<commit_after>import socket, select

sockets = [] 
port = 8001

names = {}

def router():
	server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	server_socket.bind(("", port))
	server_socket.listen(10)
	
	sockets.append(server_socket)
	print("Server started on port %s" % port)

	while True:
		# check if there are any changes
		ready_to_read,ready_to_write,in_error = select.select(sockets,[],[],0)

		for sock in ready_to_read:
			if sock == server_socket: 
				new_sock, addr = server_socket.accept()
				sockets.append(new_sock)
				print("Client (%s, %s) connected" % addr)
			else:
				try:
					# get messages from the client
					data = sock.recv(4096).decode()
					if data:
						data = str(data)
						# there is something in the socket
						if data[:5] == "/name":
							names[data[6:]] = sock
						else:
							send(sock, data)  
					else:
						# remove the socket that's broken    
						if sock in sockets:
							sockets.remove(sock)

						# at this stage, no data means probably the connection has been broken
						#broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr) 
				except:
					#broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
					continue

	server_socket.close()

def send(sender, data):
	# find sender name
	sender_name = "OOPS"
	for key, val in names.values():
		if val == sender:
			sender_name = key
	sep = data.find(":")
	receiver = data[:sep]
	msg = data[sep+1:].strip()
	if receiver in names:
		names[receiver].send(("%s: %s" % (sender_name, data)).encode())
	else:
		sender.send(("Could not find user %s" % receiver).encode())
	
if __name__ == "__main__":
	router()
{'fraction_non_alphanumeric': 0.1307824100513992, 'fraction_numerical': 0.00885208452312964, 'mean_word_length': 3.3623910336239105, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3887075', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1189, 'n_words': 334}