M
M
Mobyman2011-02-27 13:01:40
Python
Mobyman, 2011-02-27 13:01:40

multiuser python server

Good day, the university gave the task to make a fault-tolerant system. I preferred to write it in python.

But since the language is new to me, the second day I write on it, questions arise:
How to create a server application that will work simultaneously with two or more clients, plus an alternative (for fault tolerance) second server.

Those. more or less like this:

  
      Alternate <----------> Primary
             server server   
                                                            / | \
                                                      Client Client Client


I read something about twisted, something about select, but in the end I did not understand how and what.
Here is my server code:
# -*- coding: cp1251 -*-
import socket, string, threading, sys

def sync(sock):
  sock.send('sync ok!')
  
def alt(num):
  print """
  ====================
    AltServer thread ok
  ====================
  """
      
def listen(port, first = False, num = 0):
  srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  srv.bind(("localhost", port))

  while 1:
      print "Listen...", port
      print "Thread: ", threading.currentThread(), " Num: ", num, "\n" 
      srv.listen(1)             
      global sock
      sock, addr = srv.accept()
      while 1:
        rec = sock.recv(1)
        if not rec: 
          break
        else:
          if (first):
            while 1:
              num = sock.recv(16)
              if not num: 
                break
              else:
                print "Number rcvd success! (", num ,")" 
                sock.send("ok")
                proc2 = threading.Thread(target=listen, name="cli2", args=[30001, False, num])
                proc3 = threading.Thread(target=listen, name="cli3", args=[30002, False, num])
                proc4 = threading.Thread(target=alt, name="altserver", args=[num])
                proc2.start()
                proc3.start()
                proc4.start()
              
          else:
            sock.send(num)
            status = sock.recv(16)
            while 1:
              if not status: 
                break
              else:
                if (status == "ok"):
                  sync(sock)
                else:
                  sync(sock)
                  print "error, pack:", status
                  break
        print "From %s:%s:" % addr, rec
        
      sock.close()
  
def main():
  proc1 = threading.Thread(target=listen, name="cli1", args=[30000, True])
  proc1.start()

main()
sys.exit(0)


The very essence of the program is the calculation of the Fibonacci number.
The client that connects first sends the number entered by the user (Fibonacci number), then the server sends this number to the other connected clients, then each client counts the next iteration and sends it to the server, the server compares the results, and uses the principle of voting (if some client sent unexpected data, then its response is considered incorrect, this is recorded in the log and the client is forcibly disconnected). It is worth considering the failure of the main server, if this happens, then the clients should automatically connect to the alternate server and continue the calculation.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DeNnEr, 2011-02-28
@Mobyman

From the very beginning, you went in the right direction - Twisted.
If you really want a "nimble, fault-tolerant server", then the best option is to write such an application on Twisted, as a library designed for this purpose. Moreover, there are enough examples (http://twistedmatrix.com/documents/current/core/examples/#auto0)
If you just need to simulate such a system, then you need to make a normal workflow.
I would advise you to read about TCP, then it will save you from synchronization problems, tk. the algorithm will be much simpler (hint: google how UDP and TCP differ).
In general, judging by the fact that you are writing the system in a language that you have not studied (that is, you have not been taught it?), it means that the teacher simply gave a random task to keep you busy with something interesting. In this case, all the more I advise you to study Twisted, i.e. you will learn more new things, there will be experience and you will be able to tell about more.
If you were taught Python, but you skipped it, then ... Well, it's a bad thing, learn Twisted anyway or run around the forums, decant the solution. And yes, if you have been taught Python, be kind, tell me what kind of educational institution.

S
Sergey, 2011-02-27
@seriyPS

Oh, what a horror X_X…
It would be better to do this kind of tasks in Erlang, but since we decided on python…
For a start, do you have both the server(s) and clients – all in one process? Just in different threads? This is not very similar to a fault-tolerant system ...
Further, since your server only deals with the distribution of settlements between clients, then God himself ordered to use Twisted here.
In general, you would write straight 1 in 1 the wording of the task and the name of the subject to which this task is, in order to understand exactly what is needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question