If block is not well located, how to solve?

Asked

Viewed 56 times

1

I did so, by the way he’s making a mistake, because door 80 is open and he returns as closed

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server = '201.27.188.138'
port = '80'

def MeuScanDaorao(port):
    try:
        s.connect((server, port))
        return True
    except:
        return False

if MeuScanDaorao(port):
    print(MeuScanDaorao('aaa'))
else:
    print('Ta fechada')

This my code from the bottom works, I do not understand why the top not. Python is very cool, I need to learn only

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server = '201.27.188.138'

def MeuScanDaorao(port):
    try:
        s.connect((server, port))
        return True
    except:
        return False

for x in range(80,81):
    if MeuScanDaorao(x):
        print('Porta', x, 'ta aberta')
    else:
        print('Porta', x,'fechada')
  • What would be MeuScanDaorao('aaa')?

  • Just to show him if the door’s open

  • But you are calling the function to connect on the server by port 'aaa'? It didn’t make much sense.

  • That’s just the print kkk

  • Yes, even if it is a print, the function will be called and will continue to make no sense. It would not be only print("aaa")?

  • o ' and " do not change at all, the two are to comment, if you had written with nothing, just inside the paratenses, then yes it would print the function itself

Show 1 more comment

2 answers

1

As I commented, it makes no sense for you to call the function to the door 'aaa'. I tried for the comments, but it seems to have been inefficient.

Your code is like this:

if MeuScanDaorao(port):
    print(MeuScanDaorao('aaa'))
else:
    print('Ta fechada')

Which can be translated to:

  1. Try to connect on port 80, port;
  2. Connect successfully, display the result of MeuScanDaorao('aaa');
  3. If not, display 'Ta fechada';

If you analyze item 2, you will notice that what will be displayed is the return of the function call MeuScanDaorao at the door 'aaa', which doesn’t make any sense. Probably the result of MeuScanDaorao('aaa') will always be False, unless the server has a port 'aaa'.

Instead of making these confusions with function calls, why don’t you start with the simple?

if MeuScanDaorao(port):
    print('Porta está aberta')
else:
    print('Porta está fechada')

If you still show that it’s closed... well, it’s likely that it’s closed anyway.

  • Very well noted Anderson and thank you very much, but even so the answer comes back as "closed", if you run my script I did in range on pycharm with your external ip on port 80, it will show q that door is open, because that door is standard be open

0

It seems to me that your intention is to make yours if is outside the scope of the function.

Your block if must be identified at the same level as def, look at you:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server = 'externo'
port = '80'

def MeuScanDaorao(port):
    try:
        s.connect((server, port))
        return True
    except:
        return False

if MeuScanDaorao(port):
    print( MeuScanDaorao('aaa') )

Note that in the Python 3.6, print wait parenthesis.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.