TCP客户端连接脚本
我想知道是否有人知道如何为我编写一个可以接受TCP连接的脚本 如果有可能在客户端连接发生时,它会发送一个显示时间的响应,然后关闭连接,这可能会帮助您:TCP客户端连接脚本,tcp,scripting,Tcp,Scripting,我想知道是否有人知道如何为我编写一个可以接受TCP连接的脚本 如果有可能在客户端连接发生时,它会发送一个显示时间的响应,然后关闭连接,这可能会帮助您: # date_time_server.py import socket from datetime import datetime ip = "0.0.0.0" port = 8000 server = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM) server.
# date_time_server.py
import socket
from datetime import datetime
ip = "0.0.0.0"
port = 8000
server = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
server.bind((ip, port))
server.listen()
send_html_response = True
print("Server started on addresss:", ip, "port:", port)
while True:
conn, addr = server.accept()
print("replying to", addr)
if send_html_response:
response = str('HTTP/1.1 200 OK\nContent-Type: text/html\n\n' + str(datetime.now())).encode()
else:
response = str(datetime.now()).encode()
conn.send(response)
conn.close()
只需使用python3.5 date\u time\u server.py运行它,然后使用本地系统的浏览器连接到127.0.0.1:8000进行测试,当前日期时间将显示在浏览器中