这是一个简单的TCP服务器,可以在localhost上运行,并且可以监听端口4160.但是,当我执行代码时,我遇到了问题。
我使用的服务器代码是:
import socket
import threading
bind_ip = "127.0.0.1"
bind_port = 4160
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))
def handle_client(client_socket):
request = client_socket.recv(1024)
print('[*] Received: %s' % request)
message = "ACK"
client_socket.sendto(message.encode('utf-8'),bind_ip,4160)
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()
显示的错误是:
[*] Listening on 127.0.0.1:24166
[*] Accepted connection from: 127.0.0.1:57455
[*] Received: b'POST /key/1/health HTTP/1.1\r\nHost: localhost:4160\r\n.....
Exception in thread Thread-12:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\lib\threading.py", line 914, in _bootstrap_inner
self.run()
File "C:\Program Files (x86)\Python35-32\lib\threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "F:/Project Folders/EthicalHacking/Server.py", line 20, in handle_client
client_socket.sendto(message.encode('utf-8'),bind_ip,4160)
TypeError: an integer is required (got type str)
对此的任何帮助将不胜感激。
答案 0 :(得分:0)
我认为您不必使用sendto功能只需使用send:
client_socket.send(message.encode('utf-8'))