6 python sockets networking network-programming socket.io
我有这个简短的程序:
import sys
import socket
target = "google.co.uk"
port = 443
print(target)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(target)
print("successfull connection to: " + target)
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我得到:
s.connect(target)
TypeError: getsockaddrarg: AF_INET address must be tuple, not str
Run Code Online (Sandbox Code Playgroud)
当我尝试将该行更改为:s.connect(target,443)
我也收到错误:
s.connect(target,443)
TypeError: connect() takes exactly one argument (2 given)
Run Code Online (Sandbox Code Playgroud)
问题是什么?
函数接收的参数是一个元组,因此应该将元组作为参数给出。意思是f(a,b)调用该函数f((a,b))
因此,我们像这样修复您的代码:
import sys
import socket
target = "google.co.uk"
port = 443
print(target)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((target, port))
print("successfull connection to: " + target)
Run Code Online (Sandbox Code Playgroud)