User Tools

Site Tools


Writing /var/lib/dokuwiki/data/meta/teaching/ie0117/proyectos/2013/ii/proyecto_gps_-_drivecr/server.meta failed
teaching:ie0117:proyectos:2013:ii:proyecto_gps_-_drivecr:server

DriveCR_server.py

Commands for login and sign up are implemented here, along with message display methods for debugging and to inform about the current status of the server and the processes that have been executed.

#!/usr/bin/env python
import socket
import DBmethods as dbm
 
class Client_server:
 
	def __init__(self, client_socket):
		self.client_socket = client_socket
		self.ratio = 0.03
		self.username = ""		
 
	def __del__(self):
		del self.client_socket
 
	def decide(self):
		self.client_socket.settimeout(900)
                print "aqui estoy"	
		while True:
			try:
				msg = self.client_socket.recv(4096)
                                print msg 
			except socket.timeout:				
				self.client_socket.close()
				break
			instruction = []		
			instruction = msg.split(',')
			command = instruction[0]
                        print len(command)
			if command == 'printf':
				self.printf(instruction)
                                print "Voy mal"		
			elif len(command) == 20:
                                print "Voy mal"
				self.login(instruction)
			elif len(command) == 24:
                                print "Voy bien"
				self.signup(instruction)
            
 
	def answer(self, answer):				
		self.client_socket.sendall(answer)
 
	def printf(self, msg_list):	
		print msg_list[1]
		self.answer("Success")
 
	def login(self, msg_list):
                a=dbm.get_password(msg_list[1])
                print msg_list[1], msg_list[2], a
		if msg_list[2] == a:
			self.answer("login<>success")
			self.username = msg_list[1]
                        print "Bien"
		else:
			self.answer("login<>invalid username/password combination") 
 
	def signup(self, msg_list):
                print "Todo perfecto"
		status = dbm.write_user(msg_list[1], msg_list[2], msg_list[3], msg_list[4])
		if status:
			self.answer("signup<>success")
		else:
			self.answer("signup<>invalid username")

server.py

This is the code for the server that is implemented in this proyect. Basically it creates a socket for users to connect to it, so they can be listened by the server, and it allows for the use of commands and methods for exchanging information.

#!/usr/bin/env python
import socket, os
import DriveCR_server
from DriveCR_server import Client_server

#

def socket_setup():
	server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	server_address = ('localhost', 10101)
	server.bind(server_address)
	server.listen(1)
	print "server created and configured"
	return server
 
def wait_connection(server):
	wait_connection = True
	while wait_connection:	
		print "Online and waiting for new requests"	
		client_socket, client_address = server.accept()  
		newproc = os.fork()  
		if newproc == 0:	
			wait_connection = False
			client_sver = Client_server(client_socket)
			client_sver.decide()	
			client_sver = None
			print 'lost connection'
			os._exit(1)		
 
def main():
	server = socket_setup()
	wait_connection(server)
        
        data = sc.recv(1024)
        print data
    
        instruction=[]
        instruction=data.split(',')
        command = instruction[0]
        username = instruction[1]
        name = instruction[2]
        passwd = instruction[3]
        email = instruction[4]
        print command, username, name, passwd, email
 
if __name__== "__main__":
	main()

Return to previous page

teaching/ie0117/proyectos/2013/ii/proyecto_gps_-_drivecr/server.txt · Last modified: 2022/09/20 00:08 (external edit)