0

So I got this book Violent Python and well knowing just a little bit of Python 3 figured I get this to learn more about Network Programming with Python. Now realizing that the book uses an older version of python I wanted to learn to learn to get the code to work on 3. So I did the basic stuff like changing the print statements and what not but can't seem to figure out what else is wrong with the code. any help would be appreciated.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
import os
import sys


def retBanner(ip, port):
    try:
        socket.setdefaulttimeout(2)
        s = socket.socket()
        s.connect((ip, port))
        banner = s.recv(1024)
        return banner
    except Exception:
        return


def checkVulns(banner, filename):
    f = open(filename, 'r')
    for line in f.readlines():
        if line.strip('\n') in banner:
            print(('[+] Server is vulnerable: ' +
                   banner.strip('\n')))


def main():
    if len(sys.argv) == 2:
        filename = sys.argv[1]
        if not os.path.isfile(filename):
            print(('[-] ' + filename +
                   ' does not exist.'))
            exit(0)

        if not os.access(filename, os.R_OK):
            print(('[-] ' + filename +
                   ' access denied.'))
            exit(0)
    else:
        print(('[-] Usage: ' + str(sys.argv[0]) +
               ' <vuln filename>'))
        exit(0)

    portList = [21, 22, 25, 80, 110, 443]
    for x in range(147, 150):
        ip = '192.168.95.' + str(x)
        for port in portList:
            banner = retBanner(ip, port)
            if banner:
                print(('[+] ' + ip + ' : ' + banner))
                checkVulns(banner, filename)


if __name__ == '__main__':
    main()
  • 1
    What is the code supposed to do? What is happening instead? – Mr. T Feb 25 '18 at 06:10
  • 1
    The differences between Python 2 vs 3 aren't always trivial or cosmetic, indeed, the reason the two are considered different languages is because 3 is meant to have backwards compatibility-breaking changes. Suffice it to say, though, this isn't a code translation service. You should bring your question in a state of having been broken down just a little at least. Provide a [mcve]. At the very least, you should provide a description of your inputs and expected outputs, and an explanation in words of what your code is suppose to do. And all error messages, and things you've tried. – juanpa.arrivillaga Feb 25 '18 at 06:31

1 Answers1

0

Just do 2to3 script from python directory. If don't have install it with pip install 2to3 or python -m pip install 2to3. Remember that you must run that script from command line like CMD on Windows and Bash on Unix [also called Linux] systems and specify script! More info at 2to3 Python Docs.

UltraStudioLTD
  • 300
  • 2
  • 14