Need help with Port Scanner project?

Hello, I am writing a port scanner for practice in making my own penetration testing tools. I wrote the code, and it does things great, however I can’t seem to figure out how to get it to print the results out to a file.

This is my code:

from os import write import socket from IPy import IP def scan(target): converted_ip = check_ip(target) print(\n + '[...Scanning Target..] ' + str(target)) for port in range(1, 9999): scan_port(converted_ip, port) def check_ip(ip): try: IP(ip) return(ip) except ValueError: return socket.gethostbyname(ip) def get_banner(s): return s.recv(1024) def results(results): results.write(scan, "scanresults.txt") print("[..writing results to file..]") def scan_port(ipaddress, port): try: sock = socket.socket() sock.settimeout(0.2) sock.connect(((ipaddress, port))) try: banner = get_banner(sock) print('[+] Open Port ' + str(port) + ' : ' + str(banner.decode().strip("\n"))) except: print('[+] Open Port ' + str(port)) except: pass targets = input('[?] Enter targets (split mult targets with ,): ') if ',' in targets: for ip_add in targets.split(','): scan(ip_add.strip(' ')) else: scan(targets)

This is my problematic code that I can’t figure out what to do with:

def results(results): results.write(scan, "scanresults.txt") print("..writing results to file..")

I don’t know really what I’m doing and I don’t know how to fix it to get it to do what I want it to do. Can anyone give me advice on what I need to do to fix this program and explain it to me so I can implement it in my other programs I write too?

That one was just a half baked attempt to try to get it to do what I wanted.
I really also want to implement a print statement that runs while the program is writing to the file to let the user know what the program is doing…

All ideas and suggestions and explanations are welcome!