Python Forum

Full Version: socket.connect question about parenthesis
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I have been starting out with python over the past 8 months. I know I'm just at the start of my journey, and this is the first programming language that I have been working with.

I am currently playing around with socket.
My question is simple, why do I need double parenthesis sock.connect((ipaddress, port)), to get my program to work?

import socket
#from IPy import IP

ipaddress = input("Enter target to scan: ")
port = 80

#Esablish a connection to a port using socket
try:
    sock = socket.socket()
    sock.connect((ipaddress, port))
    print("[+] Port 80 is open: ")
except:
    print("[-] Port 80 is closed: ")
The answer is simple, the method socket.connect.connect() takes a single argument, the address where the socket should connect. For a socket of the AF_INET family type (the default), the address is a tuple with two elements (host, port). So you are not using double parentheses, you are passing a single argument which is a tuple.