所以我一直在尝试使用书中的指令将粗tcp客户端的数据包发送到原始tcp服务器(我有很少的python经验,但我确实有编程经验)。但是,每当我尝试发送数据包时,它有时会发生错误:
Tcp客户端:
import socket
target_host = "0.0.0.0"
target_port = 9999
# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#connect the client
client.connect((target_host,target_port))
#send some data
client.send("GET / HTTP/1.1\nHost: google.com\r\n\r\n")
#recieve some data
response = client.recv(4096)
print response
Tcp服务器:
import socket
import threading
bind_ip = "0.0.0.0"
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip,bind_port))
server.listen(5)
print "[*] Listening on %s:%d" % (bind_ip,bind_port)
# this is our client handeling thread
def handle_client(client_socket):
# priint out what client says
request = client_socket.recv(1024)
print "[*] Recieved %s" % request
#send back a packet
client_socket.send("ACK!")
client_socket.close()
while True:
client,addr = server.accept()
print "[*] Accepted connection from %s:%d" % (addr[0],addr[1])
# spin up on our client thread to handle incoming data
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start()
错误:
[GCC 5.3.1 20151205]
Python Type "help", "copyright", "credits" or "license" for more information.
[evaluate TCP client.py]
Traceback (most recent call last):
File "/root/TCP client.py", line 10, in <module>
client.connect((target_host,target_port))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
我该怎么办?