#!/usr/bin/python
# import python modules
from socket import error as SocketError
from socket import *
HOST = '192.168.1.88' # '' means bind to all interfaces
PORT = 1314 # port
# create our socket handler
s = socket(AF_INET, SOCK_STREAM)
# set is so that when we cancel out we can reuse port
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# bind to interface
s.bind((HOST, PORT))
# print we are accepting connections
print "Listening on %s:%s" % (str(HOST),str(PORT))
# listen for only 1 connection
s.listen(1)
# accept connections
conn, addr = s.accept()
# print connected by ipaddress
print 'Connected by', addr
print "Data collected"
while True:
print "Loop initiated"
command = raw_input("Enter shell command or quit: ")
try:
conn.send(command)
if command == "quit":
break
else:
print(conn.recv(1024))
except SocketError as e:
print e
conn.close()
忽略混乱的代码或其他内容,但是有人可以解释是什么导致了问题中的错误?我怀疑这与setsockopt有关,但老实说我没有任何线索。感谢您的帮助。