標籤:伺服器 python import listen
import sockettarget_host = "127.0.0.1"target_port = 9999client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)client.connect((target_host,target_port))client.send("GET / HTTP/1.1\r\nHost:baidu.com\r\n\r\n")response = client.recv(4096)print responseclient.close()
這個是用戶端口:
import socketimport threadingbind_ip = "0.0.0.0"bind_port = 9999server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)server.bind((bind_ip,bind_port))server.listen(5)print "[*] listen on %s:%d" % (bind_ip,bind_port)def handle_client(client_socket): request = client_socket.recv(1024) print "[*] Received %s" % request client_socket.send("OK") client_socket.close()while True: client,addr = server.accept() print "[*] Accepted connection from: %s:%d" % (addr[0],addr[1]) client_handler = threading.Thread(target=handle_client,args=(client,)) client_handler.start()
這個是服務連接埠:
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/7E/A8/wKioL1cGbd7zkyAwAAA9hw3BkLE144.png" title="QQ20160407222253.png" alt="wKioL1cGbd7zkyAwAAA9hw3BkLE144.png" />
這個是啟動並執行結果。
怎麼樣,我厲害吧,哈哈!
本文出自 “專註php” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1761520
用python類比TCP 伺服器跟 TCP串連