Python Forum
socket.connect question about parenthesis
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket.connect question about parenthesis
#1
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: ")
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python socket connect only sometimes espDino 0 1,501 Jul-15-2020, 12:13 PM
Last Post: espDino
  Socket won't connect, giving me a typeerror GalaxyCoyote 1 3,349 Apr-07-2020, 10:28 AM
Last Post: Mateusz
  Question concerning function of a a socket pkm 4 3,817 Jun-04-2019, 07:48 AM
Last Post: DeaD_EyE
  UDP Socket Basic question zeeshannnetwork 2 3,078 Sep-10-2018, 02:25 AM
Last Post: zeeshannnetwork

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020