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

标签 python sockets networking socket.io network-programming

我有这个简短的程序:

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/

相关文章:

c++ - 在 Visual C++ 中接受来自客户端(套接字编程)的连接的问题

linux - 如何从 web 发送参数或指令到 linux 机器并在 web 中显示结果?

python - Pipenv:无条件安装平台特定包的依赖项?

python - 有没有办法改变ezdxf中的背景颜色?

sockets - 为什么 Sendto() 系统调用不返回发送的字节数?

c# - 适用于 Windows 和 Linux 的同一台机器上的套接字

Java套接字编程

c - 数据报套接字中的单播源地址过滤

Python取消引用URL但保留超链接

python - 扭曲的 adbapi cp_reconnect 不工作