gpt4 book ai didi

python套接字错误: AF_INET address must be tuple,不是str

转载 作者:行者123 更新时间:2023-12-01 02:11:56 25 4
gpt4 key购买 nike

我有这个简短的程序:

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)

当我运行代码时,我得到:

    s.connect(target)
TypeError: getsockaddrarg: AF_INET address must be tuple, not str

当我尝试将该行更改为:s.connect(target,443)我也遇到了一个错误:

    s.connect(target,443)
TypeError: connect() takes exactly one argument (2 given)

问题是什么?

最佳答案

函数接收的参数是一个元组,因此应该将元组作为参数给出。意思是 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)

关于python套接字错误: AF_INET address must be tuple,不是str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48651321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com