python 基本socket
来源:互联网 发布:ug多轴编程视频 编辑:程序博客网 时间:2024/05/01 12:06
socket()函数
Python 中,我们用 socket()函数来创建套接字,语法格式如下:
socket.socket([family[, type[, proto]]])
参数
family: 套接字家族可以使AF_UNIX或者AF_INET
type: 套接字类型可以根据是面向连接的还是非连接分为SOCK_STREAM或SOCK_DGRAM
protocol: 一般不填默认为0.
Socket 对象(内建)方法
服务器端套接字
客户端套接字
公共用途的套接字函数
实例:
服务端:
>>> import socket>>> s =socket.socket()>>> host="127.0.0.1">>> port=12345>>> s.bind((host,port))>>> s.listen(10)>>> client,addr=s.accept()>>> addr('127.0.0.1', 11117)>>> client<socket.socket fd=620, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 12345), raddr=('127.0.0.1', 11117)>>>> client.send("hello")Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> client.send("hello")TypeError: a bytes-like object is required, not 'str'>>> client.send(bytes(123))123>>> client.send(bytes('hello'))Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> client.send(bytes('hello'))TypeError: string argument without an encoding>>> client.send(bytes('hello'.encode()))5>>>
客户端:
>>> import socket>>> s=socket.socket>>> s=socket.socket()>>> s.connect(("127.0.0.1",12345))>>> print s.recv(1024)SyntaxError: invalid syntax>>> print (s.recv(1024))b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'>>> while True: print(s.recv(1024))b'hello'
阅读全文
0 0
- python---基本socket编写
- python 基本socket
- python socket编程基本操作
- python的socket基本使用
- python中socket基本编程
- python --- 基本socket通信控制
- Python 下基本的 socket 编程
- python中socket【TCP】基本编程知识
- python socket基本过程之一(client)
- python socket 编程01-实现基本点对点通信
- python socket基本过程之二(server)
- python---基本的socket通信与字典组合运行
- Python Socket
- Python socket
- socket python
- python socket
- Python Socket
- python socket
- Git入门与进阶
- Java并发编程:volatile关键字解析
- Unity3D基于OnGUI实时显示FPS
- 九鼎之尊一
- maven跳过单元测试-maven.test.skip和skipTests的区别
- python 基本socket
- 运算符和输出函数
- Java内存区域理解
- Codeforces 876C Classroom Watch
- 如何基于TCP/IP协议进行MFC Socket网络通讯编程
- 浅谈存储系统的故障处置基本思路
- 解决SpringBoot多工程时jar包中注解不能扫描生效问题
- windows命令行生成文件夹目录树状结构
- 使用STL在DB块中寻找相同字符