python中9步教会你socket连接web之终极版
导入socket 和 os 模块自不必说
import socket
import os
1.socket连接
server_sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM)`
参数为socket.AF_INET 是基于网络的,sock.SOCK_STREAM基于TCP协议
2.socket监听
server_sk.bind(("127.0.0.1",8888))
(127.0.0.1是本地ip地址)
参数为(地址,端口号)
3.socket的最大并发数量
server_sk.listen(128)
也就是最多能有同时几个访问
应该吧 嘻嘻嘻
定义了三个函数
这里我定义了三个,当然可以定义多个函数。看你需要几个子页面
def index(url):
with open('index.html','r',encoding='utf-8')as f:
data=f.read().replace('xxx',url).encode()
return data
def hero(url):
with open('hero.html','rb')as f:
data=f.read()
return data
def error(url):
with open('error.html','rb')as f: