B
B
bot123123 robotovkiy1232020-04-27 06:13:24
Python
bot123123 robotovkiy123, 2020-04-27 06:13:24

How to pass ip from text file to sock.connect?

I wrote a simple code in which I want a
connect = sock.connect((ip,port))
string with ip from a text file to be passed to a variable, namely ip.
The code itself:

import time
import socket

start_time = time.time()

def scan_port(ip,port):
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.settimeout(0.01)
  try:
     print(ip)
     connect = sock.connect((ip,port))
     print(ip + port + ' open.')
     connect.close()
  except Exception as e:

     print(e)

ports = ['443']

for port in ports:
    file_result_ips = open('result_ips.txt', 'r')
    for ip in file_result_ips: 
         scan_port(int(ip.rstrip('\r\n')),port) #убираю перенос строки (.rstrip('\r\n')) и привожу к типу int 
#        scan_port(ip,port)

file_result_ips.close()

print('')
print('%s seconds' % (time.time() - start_time))

Mistake:
File "test.py", line 24, in <module>
    scan_port(int(ip.rstrip('\r\n')),port)
ValueError: invalid literal for int() with base 10: '185.71.67.88'

If you pass ip to the scan_port function without any manipulations, like this "scan_port (ip, port)", then the following error appears:
185.71.67.88

an integer is required (got type str)
45.60.40.164

an integer is required (got type str)

Where to google to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-04-27
@bot123123

So we run and read the docs, it says what types this pair should have https://docs.python.org/3/library/socket.html#sock...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question