Answer the question
In order to leave comments, you need to log in
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))
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'
185.71.67.88
an integer is required (got type str)
45.60.40.164
an integer is required (got type str)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question