1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| import socket import threading
COOKUE = '''Cookie: Idea-38dabdcb=1a3e1891-7313-4a6c-8141-c158de42e97''' LANGUAGE = '''Accept-Language: zh-CN,zh;q=0.8''' USER_AGENT = '''User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36''' ACCEPT = '''Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8''' CACHE_CONTROL_MAX_AGE = '''Cache-Control: max-age=0''' CONNECTION_KEEP_ALIVE = '''Connection: keep-alive''' HOST = '''Host: 192.168.0.101:8000''' PATH = '''/blog/'''
BICRLF = "\r\n\r\n" CRLF = "\r\n" CONTENT_LEN = "Content-Length: " send_flag = True
def send_data(tcp_socket): while send_flag: len_msg = int(input("how many char do you wanna send? : ")) comm, content, req_med = input("comm :").split(" ") if req_med == "p": req_med = "POST" else: req_med = "GET" long_msg = content * len_msg len_lmsg = str(len(long_msg)) msg = ("%s %s HTTP/1.1" + CRLF + "%s%s%s%s%s%s%s%s") \ % (req_med, PATH, HOST + CRLF + CONTENT_LEN + len_lmsg + CRLF, CONNECTION_KEEP_ALIVE + CRLF, CACHE_CONTROL_MAX_AGE + CRLF, ACCEPT + CRLF, USER_AGENT + CRLF, LANGUAGE + CRLF, COOKUE + BICRLF, long_msg) msg = msg.encode("utf-8")
if comm.find("close") != -1: tcp_socket.shutdown(socket.SHUT_RDWR) elif comm.find("pass") != -1: pass elif send_flag == False: return else: tcp_socket.send(msg)
def recv_data(tcp_socket=None, len_recv=None): while True: recv = tcp_socket.recv(len_recv) if recv: global send_flag send_flag = True else: send_flag = False tcp_socket.shutdown(socket.SHUT_WR) return print(recv)
def main(): while True: tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print(tcp_socket.fileno())
tcp_socket.connect(("192.168.0.101", 8000)) global send_flag send_flag = True len_recv = 6148 thread = threading.Thread(target=recv_data, args=(tcp_socket, len_recv)) thread.start() send_data(tcp_socket)
if __name__ == "__main__": main()
|