You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
teamwk123/python/tcpqzy.py

60 lines
1.3 KiB

4 weeks ago
# @Coding: utf-8
# @Time: 2021/8/3 3:13
import socket
import os
HOST = "0.0.0.0"
PORT = 8383
so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 建立Socket连接, AF_INEF说明使用IPv4地址, SOCK_STREAM指明TCP协议
so.bind((HOST, PORT))# 绑定IP和端口
so.listen(1)# 监听
#print(f"listen: {so.listen(1)} connected")
conn, addr = so.accept()# 接收TCP连接, 并返回新的Socket对象
#print(f"Client: {addr} connected")# 打印客户端的IP
message = "Connection Success"#用于确定连接成功
conn.send(message.encode('utf8'))# 向客户端发送当前的时间, send()函数接收的参数为bytes类型,
#data = conn.recv(1024).decode()# 接收客户端发送的数据
#data1 = conn.recv(1024)# 接收客户端发送的数据
#print(data1)
#l=data.split(' ')
#step1:username
d1=conn.recv(1024)
#l1=data1.split(b' ')
l1=d1.split(b' ')
print(l1)
username=l1[1].decode()
conn.send(b'username')
d3=conn.recv(1024)
l3=d3.split(b' ')
time = l3[1].decode()
#conn.send(b'filename')
print('time:',time)
conn.send(b'time')
#step4:content
f=open(os.path.dirname(__file__)+"//info.html",'a')
f.write(username + ' ' + time + '<br/>')#写啥变量写进文件
f.close()
#f.close()
conn.close()# 关闭连接
'''
f.write(content1)
f.close()
'''