0

I want to write a code to achieve below

I have written a client(c1) and 3 servers (s1, s2 and s3) in python.

C1 will check if it is able to connect to s1. If yes, it will sleep for 2 mins. It will wake up again after 2 mins and will check if s1 is connectable. If no, then it will check s2. If s2 is listening then c1 will go to sleep for 2 mins. And this will keep on happening.

Now, my issue is, I can check s1 but not able use if else coding

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    try:
        s.connect(('111.111.1.1', 2222))
        time.sleep(120) 
    except Exception as inst:
        print("not ready1")   
        try:
            s.connect(('11.22.22.2', 2212))
            time.sleep(120) 
        except Exception as inst:
            print("not ready2")
            try:
                s.connect(('1.1.1.33', 1234)) 
                time.sleep(120) 
            except Exception as inst:
                print("not ready3") 

I want to achieve this using if else looping. And also make sure that this code executes infinite time. Right now, it will execute only once.

4
  • Using except Exception as e: is discouraged, it's not a good idea. I'm not sure I understand what the issue is, can you be more specific? Commented Jan 7, 2020 at 21:20
  • When you want to check s3 ? Commented Jan 7, 2020 at 21:21
  • I just want to check if c1 is able to connect to s2 or not. If not then c1 should check s3. Commented Jan 7, 2020 at 21:23
  • @Newbie: Please edit your question. Commented Jan 7, 2020 at 21:23

2 Answers 2

2

Would that fit you?

import socket
import time


def run(ip, port):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((ip, port))
        return True
    except Exception:
        return False


while True:
    if run('111.111.1.1', 2222):
        print('s1 read')
        time.sleep(120)
    elif run('11.22.22.2', 2222):
        print('s2 read')
        time.sleep(120)
    else:
        print('s1 and s2 not read')
Sign up to request clarification or add additional context in comments.

2 Comments

I am using the function as a thread. Not sure if I can do this using threads
I have no problem using with thread, but you will need to change this algorithm
0

edit: after thinking about this, you will hit recursion depth with the old result, you would have to use a loop with the code as presented. there is another answer that shows a good result for that


could you put the code in a function and call recursively? I'm not 100% clear on what you want to do, I think you want to check 3 servers, 1 at a time 2 minutes apart.

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
def recursive_function():
    try:
        s.connect(('111.111.1.1', 2222))
        time.sleep(120) 
    except OSError as inst:
        print("not ready1") 
        try:
            s.connect(('11.22.22.2', 2212))
            time.sleep(120) 
        except OSError as inst:
            print("not ready2") 
    return recursive_function()
recursive_function()

Also, as others have posted, catching Exception is not a good idea unless you specifically need to catch that (usually for cleanup then raise the exception again) there is lots of room for improvement in this function as well, I just made it 'passible' but I would not say this is a good result.

you may want to read up on functional programming methods, it sounds like this is what you are trying to do.

2 Comments

You can't have an infinite recursive loop in Python, in case all servers are responding forever. I guess you are running into an maximum recursion depth exceeded error.
Is OSError the right exception to check for in this case? I'm not very familiar with the socket library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.