没有合适的资源?快使用搜索试试~ 我知道了~
socket编程1
需积分: 0 0 下载量 184 浏览量
2022-08-04
16:20:03
上传
评论
收藏 2.1MB PDF 举报
温馨提示
![preview](https://dl-preview.csdnimg.cn/86334285/0001-f885267fbe990e9bb6135cce94443712_thumbnail.jpeg)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
试读
24页
2、IO多路复用 3、socketserver 1、file模块是针对某个指定文件进行【打开】【读写】【关闭】 2、socket模块是针对 服务器端 和 客户端
资源详情
资源评论
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/86334285/bg1.jpg)
博客园 首页 新随笔 联系 订阅 管理
张岩林
路漫漫其修远兮,吾将上下而求索!
一、python系列
二、前端知识
三、Mysql系列
全屏 目录
python之socket编程
本章内容
1、socket
2、IO多路复用
3、socketserver
Socket
socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用【打开】【读写】
【关闭】模式来操作。socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数
就是对其进行的操作(读/写IO、打开、关闭)
基本上,Socket 是任何一种计算机网络通讯中最基础的内容。例如当你在浏览器地址栏中输入
http://www.cnblogs.com/ 时,你会打开一个套接字,然后连接到 http://www.cnblogs.com/ 并读
取响应的页面然后然后显示出来。而其他一些聊天客户端如 gtalk 和 skype 也是类似。任何网络通
讯都是通过 Socket 来完成的。
Python 官方关于 Socket 的函数请看
http://docs.python.org/library/socket.html
socket和file的区别:
python之socket编程 - 张岩林 - 博客园 https://www.cnblogs.com/aylin/p/5572104.html
第1页 共24页 2018/2/2 16:44
![](https://csdnimg.cn/release/download_crawler_static/86334285/bg2.jpg)
1、file模块是针对某个指定文件进行【打开】【读写】【关闭】
2、socket模块是针对 服务器端 和 客户端Socket 进行【打开】【读写】【关闭】
那我们就先来创建一个socket服务端吧
import socket
sk = socket.socket()
sk.bind(("127.0.0.1",8080))
sk.listen(5)
conn,address = sk.accept()
sk.sendall(bytes("Hello world",encoding="utf-8"))
python之socket编程 - 张岩林 - 博客园 https://www.cnblogs.com/aylin/p/5572104.html
第2页 共24页 2018/2/2 16:44
![](https://csdnimg.cn/release/download_crawler_static/86334285/bg3.jpg)
import socket
obj = socket.socket()
obj.connect(("127.0.0.1",8080))
ret = str(obj.recv(1024),encoding="utf-8")
print(ret)
socket更多功能
def bind(self, address): # real signature unknown; restored from __doc__
"""
bind(address)
Bind the socket to a local address. For IP sockets, the address is a
pair (host, port); the host must refer to the local host. For raw packet
sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])
"""
'''将套接字绑定到本地地址。是一个IP套接字的地址对(主机、端口),主机必须参考本地主机。'''
pass
def close(self): # real signature unknown; restored from __doc__
"""
close()
Close the socket. It cannot be used after this call.
"""
'''关闭socket'''
pass
def connect(self, address): # real signature unknown; restored from __doc__
"""
connect(address)
Connect the socket to a remote address. For IP sockets, the address
is a pair (host, port).
python之socket编程 - 张岩林 - 博客园 https://www.cnblogs.com/aylin/p/5572104.html
第3页 共24页 2018/2/2 16:44
![](https://csdnimg.cn/release/download_crawler_static/86334285/bg4.jpg)
"""
'''将套接字连接到远程地址。IP套接字的地址'''
pass
def connect_ex(self, address): # real signature unknown; restored from __doc__
"""
connect_ex(address) -> errno
This is like connect(address), but returns an error code (the errno value)
instead of raising an exception when an error occurs.
"""
pass
def detach(self): # real signature unknown; restored from __doc__
"""
detach()
Close the socket object without closing the underlying file descriptor.
The object cannot be used after this call, but the file descriptor
can be reused for other purposes. The file descriptor is returned.
"""
'''关闭套接字对象没有关闭底层的文件描述符。'''
pass
def fileno(self): # real signature unknown; restored from __doc__
"""
fileno() -> integer
Return the integer file descriptor of the socket.
"""
'''返回整数的套接字的文件描述符。'''
return 0
def getpeername(self): # real signature unknown; restored from __doc__
"""
getpeername() -> address info
Return the address of the remote endpoint. For IP sockets, the address
info is a pair (hostaddr, port).
"""
'''返回远程端点的地址。IP套接字的地址'''
pass
def getsockname(self): # real signature unknown; restored from __doc__
python之socket编程 - 张岩林 - 博客园 https://www.cnblogs.com/aylin/p/5572104.html
第4页 共24页 2018/2/2 16:44
![](https://csdnimg.cn/release/download_crawler_static/86334285/bg5.jpg)
"""
getsockname() -> address info
Return the address of the local endpoint. For IP sockets, the address
info is a pair (hostaddr, port).
"""
'''返回远程端点的地址。IP套接字的地址'''
pass
def getsockopt(self, level, option, buffersize=None): # real signature unknown; restored
from __doc__
"""
getsockopt(level, option[, buffersize]) -> value
Get a socket option. See the Unix manual for level and option.
If a nonzero buffersize argument is given, the return value is a
string of that length; otherwise it is an integer.
"""
'''得到一个套接字选项'''
pass
def gettimeout(self): # real signature unknown; restored from __doc__
"""
gettimeout() -> timeout
Returns the timeout in seconds (float) associated with socket
operations. A timeout of None indicates that timeouts on socket
operations are disabled.
"""
'''返回的超时秒数(浮动)与套接字相关联'''
return timeout
def ioctl(self, cmd, option): # real signature unknown; restored from __doc__
"""
ioctl(cmd, option) -> long
Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are
SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.
SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).
"""
return 0
def listen(self, backlog=None): # real signature unknown; restored from __doc__
"""
python之socket编程 - 张岩林 - 博客园 https://www.cnblogs.com/aylin/p/5572104.html
第5页 共24页 2018/2/2 16:44
剩余23页未读,继续阅读
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![avatar](https://profile-avatar.csdnimg.cn/a6dfad6d23054ff5b3eb4c401d37ba20_weixin_35806032.jpg!1)
余青葭
- 粉丝: 38
- 资源: 303
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 图标生成工具 Windows 版本(支持.ico和.icns)IconBuilder v1.1
- 666662222277777
- 图标生成工具 Mac 版本(支持.ico和.icns)IconBuilder v1.1
- 求最大公约数(简单).cpp
- elasticsearch数据库下载、配置、使用案例
- springboot的概要介绍与分析
- C语言的概要介绍与分析
- 第一个较大的Android项目,基于Android平台的图书管理系统(Android studio).zip
- Cisco Packet Tracer 6.2 for Windows Instructor Version
- 使⽤pyIAST计算⽓体吸附选择性
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论0