在 Python 中打开套接字的最佳方法 [英] Best way to open a socket in Python

查看:27
本文介绍了在 Python 中打开套接字的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Python 打开一个 TCP 客户端套接字.我是否必须完成所有低级 BSD 创建套接字句柄/连接套接字的内容,还是有更简单的单行方式?

I want to open a TCP client socket in Python. Do I have to go through all the low-level BSD create-socket-handle / connect-socket stuff or is there a simpler one-line way?

推荐答案

在 python 中打开套接字非常简单.你真的只需要这样的东西:

Opening sockets in python is pretty simple. You really just need something like this:

import socket
sock = socket.socket()
sock.connect((address, port))

然后你可以像任何其他套接字一样send()recv()

and then you can send() and recv() like any other socket

这篇关于在 Python 中打开套接字的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆