拉起http服务代码
#-*- coding: utf-8 -*-
from socket import *
from time import ctime
import threading
import os
import re
import time
import sys
from urllib.parse import unquote
def server_mod(port,host):
sock = socket(AF_INET, SOCK_STREAM)
sock.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
try:
sock.bind((host,port))
except Exception as e:
print(e)
sys.exit(0)
# 同时挂起排队的数量,默认为5
sock.listen(5)
print('waiting for connection in port %d'%port)
while True:
tcpClientSock,addr = sock.accept()
print('connect from ', addr)
data = tcpClientSock.recv(1024)
post_data = b'->|attack|<-'
Content_Length = str(len(post_data))
html_rsp_header = '''HTTP/1.1 200 OK\r\nServer: Apache/2.4.7 (Ubuntu)\r\nAccept-Ranges: bytes\r\nLast-Modified: Sat, 06 Jun 2020 15:44:20 GMT\r\nConnection: Close\r\nContent-Length: {Content_Length}\r\nContent-Type: application/binary\r\n\r\n'''.format(Content_Length=Content_Length).encode()
all_send = html_rsp_header + post_data
try:
tcpClientSock.send(all_send)
print(ctime(),'\n', data.decode().lstrip())
except Exception as e:
print("Send data Error",e)
tcpClientSock.close()
sock.close()
if __name__ == '__main__':
HOST='x.x.x.x'
PORT=80
try :
server_mod(PORT, HOST)
except KeyboardInterrupt:
sys.exit(0)