这是我的全部代码:它使用主机和端口的用户输入。
服务器端代码:
import socket
import subprocess, os
print("#####################")
print("# Python Port Maker #")
print("# #")
print("#'To Go Boldy Where'#")
print("# No Other Python #")
print("# Has Gone #")
print("# By Riley #")
print("#####################")
print(' [*] Be Sure To use https://github.com/Thman558/Just-A-Test/blob/master/socket%20client.py
on the other machine')
host = input(" [*] What host would you like to use? ")
port = int(input(" [*] What port would you like to use? "))
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((host, port))
server_socket.listen(5)
print("n[*] Listening on port " +str(port)+ ", waiting for connections.")
client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " +client_ip+ " connected.n")
while True:
try:
command = input(client_ip+ "> ")
if(len(command.split()) != 0):
client_socket.send(b'command')
else:
continue
except(EOFError):
print("ERROR INPUT NOT FOUND. Please type 'help' to get a list of commands.n")
continue
if(command == "quit"):
break
data = client_socket.recv(1024)
print(data + "n")
client_socket.close()
错误再次出现:
print(:"Recieved Command : ' +command)
TypeError: can't concat str to bytes
当用户只在给定命令时连接时,它工作得很好。当这个错误发生时,不确定它是否可能是data
变量,但这个代码不想工作Lol。这是我用来连接客户端的代码:
import socket
import subprocess, os
print("######################")
print("# #")
print("# The Socket #")
print("# Connecter #")
print("# #")
print("# By Yo Boi #")
print("# Riley #")
print("######################")
host = input("What host would you like to connect to? ")
port = int(input("What port is the server using? "))
connection_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
connection_socket.connect((host, port))
print("n[*] Connected to " +host+ " on port " +str(port)+ ".n")
while True:
command = connection_socket.recv(1024)
split_command = command.split()
print("Received command : " +command)
if command == "quit":
break
if(command.split()[0] == "cd"):
if len(command.split()) == 1:
connection_socket.send((os.getcwd()))
elif len(command.split()) == 2:
try:
os.chdir(command.split()[1])
connection_socket.send(("Changed directory to " + os.getcwd()))
except(WindowsError):
connection_socket.send(str.encode("No such directory : " +os.getcwd()))
else:
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
stdout_value = proc.stdout.read() + proc.stderr.read()
print(stdout_value + "n")
if(stdout_value != ""):
connection_socket.send(stdout_value)
else:
connection_socket.send(command+ " does not return anything")
connection_socket.close()
因此,除了数据类型不匹配之外,您的脚本还有许多错误。我会告诉你str
和byte
不匹配出了什么问题。无论何时将字符串转换为字节,都必须指定编码方法。不管怎样,我已经修改了你的剧本,剩下的事情交给你来解决。(
服务器脚本
import socket
print("#####################")
print("# Python Port Maker #")
print("# #")
print("#'To Go Boldy Where'#")
print("# No Other Python #")
print("# Has Gone #")
print("# By Riley #")
print("#####################")
print(' [*] Be Sure To use https://github.com/Thman558/Just-A-Test/blob/master/socket%20client.py on the other machine')
host = input(" [*] What host would you like to use? ")
port = int(input(" [*] What port would you like to use? "))
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((host, port))
server_socket.listen(5)
print("n[*] Listening on port " + str(port) + ", waiting for connections.")
client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " + client_ip + " connected.n")
while True:
try:
command = input(client_ip + "> ")
if len(command.split()) != 0:
client_socket.send(bytes(command, 'utf-8'))
else:
continue
except EOFError:
print("ERROR INPUT NOT FOUND. Please type 'help' to get a list of commands.n")
continue
if command == "quit":
break
data = client_socket.recv(1024).decode()
print(data + "n")
client_socket.close()
客户端脚本
import os
import socket
import subprocess
print("######################")
print("# #")
print("# The Socket #")
print("# Connecter #")
print("# #")
print("# By Yo Boi #")
print("# Riley #")
print("######################")
host = input("What host would you like to connect to? ")
port = int(input("What port is the server using? "))
connection_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
connection_socket.connect((host, port))
print("n[*] Connected to " + host + " on port " + str(port) + ".n")
while True:
print('nWaiting for a command....')
command = connection_socket.recv(1024).decode()
split_command = command.split()
print("Received command : ", command)
if command == "quit":
break
if command[:2] == "cd":
if len(command.split()) == 1:
connection_socket.send(bytes(os.getcwd(), 'utf-8'))
elif len(command.split()) == 2:
try:
os.chdir(command.split()[1])
connection_socket.send(bytes("Changed directory to " + os.getcwd(), 'utf-8'))
except WindowsError:
connection_socket.send(str.encode("No such directory : " + os.getcwd()))
else:
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
stdout_value = proc.stdout.read() + proc.stderr.read()
print(str(stdout_value) + "n")
if stdout_value != "":
connection_socket.send(stdout_value)
else:
connection_socket.send(bytes(str(command) + ' does not return anything', 'utf-8'))
connection_socket.close()
提示:大多数错误都包括逻辑缺陷!
如果打印type(data)
,您会注意到它不是一个字符串,而是一个字节实例。要连接一个新行,您可以这样写:
print(data + b"n")
socket.recv(size)
返回一个字节对象,您正试图将该对象附加到字符串中,这导致了错误。在附加换行之前,可以通过执行str(data , 'utf-8')
将套接字消息转换为字符串
print()
方法无论如何都会添加一个换行符,这样您就可以只写print(data)
,这将起的作用