Block if not well located, how to solve?

1

I did it, by the way it is giving error, because port 80 is open and it 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 is my code below, I do not understand why the one above does 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')
    
asked by anonymous 21.06.2018 / 14:45

2 answers

1

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

Your code looks like this:

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

What can be translated to:

  • Try to connect to port 80, port ;
  • If you connect successfully, display the result of MeuScanDaorao('aaa') ;
  • If not, display 'Ta fechada' ;
  • If you look at item 2, you will notice that the callback of the MeuScanDaorao function on port 'aaa' will not be displayed, which makes no sense. Probably the result of MeuScanDaorao('aaa') will always be False , unless the server has a 'aaa' port.

    Instead of making these confusions with function calls, why not start with the simple?

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

    If it still shows that it's closed ... well, it's probably closed anyway.

        
    21.06.2018 / 17:51
    0

    It seems to me that your intent is to get your if out of the scope of the function.

    Your block if must be indented at the same level as def , see:

    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 no Python 3.6 , print expects parentheses.

        
    21.06.2018 / 15:13
    ___ ___ erkimt jQuery search field datatable ______ qstntxt ___

    I'm using the jQuery Datatable component. In the preview on the computer table with the search field is ok, but when viewing the mobile field exceeds the panel where it is inserted, as can be seen in the image below:

    HowcanIresolvethis?

        
    _________azszpr308642

    Enter in your CSS style one to limit the width of %code% %:

    %pre%

    The selector %code% handle the default search datatables element. Adjust the value of %code% more or less as does not exceed the panel.

        
    ___ I can not start an array with foreach