Некоторое время назад я написал сервер на python 3.6 на машине с Windows 7, и он работал там. С тех пор я перевел сервер на linux mint 18.3 Sylvia с рабочим столом Cinnamon. По какой-то причине программа не принимает соединения, даже после изменения порта с 80 на 8080. Я определил это с помощью Internet Explorer на другом компьютере для диагностики проблем в сети (да, я поставил: 8080 после IP-адреса). Мой вопрос: что именно я должен сделать для того, чтобы программа работала на компьютере с Linux?
Вот код:
import socket
import sys
import os
from _thread import *
def filterHTML(data):
count = 0
word = ""
loc = data.find("HTTP")
req = ""
while True:
if (data[count] == ' '):
count+=1
break
else:
req+=data[count]
count+=1
while True:
if (count == loc - 1):
break
elif (data[count] == '?'):
break
else:
word+=data[count]
count+=1
return req, word
def getEnd(data):
lng = len(data)
location = 0
for i in range(0,lng,1):
if (data[i] == '/'):
location = i
newstring = ""
for e in range(location, lng, 1):
newstring+=data[e]
return newstring
def threaded_client(Connection):
while True:
print("recieving data from client")
try:
data = Connection.recv(4096)
data = data.decode('utf-8')
print(data)
com, param = filterHTML(data)
if (com == "GET"):
end = getEnd(param)
print(param)
if (param == "/"):
sendFileContent(Connection,"text","init.html")
elif (end == "/favicon.ico"):
sendFileContent(Connection,"bin","favicon.bmp")
elif (param == "/main/" or param == "/main"):
sendFileContent(Connection,"text","main.html")
elif (end == "/BSS.png"):
sendFileContent(Connection,"bin","BSS.png")
elif (param == "/Contact/"):
sendFileContent(Connection,"text","contact.html")
elif (param == "/Projects/"):
sendFileContent(Connection,"text","projects.html")
elif (end == "/cypher.png"):
sendFileContent(Connection,"bin","cypher.png")
elif (end == "/stockOrder.png"):
sendFileContent(Connection,"bin","stockOrder.png")
elif (end == "/website.png"):
sendFileContent(Connection,"bin","website.png")
elif (end == "/background.png"):
sendFileContent(Connection,"bin","background.png")
else:
sendFileContent(Connection,"text","404.html")
Connection.close()
return
except error:
print("client " + str(Connection) + "timed out")
Connection.close()
def sendFileContent(connection,ftype,fileName):
if (ftype == "text"):
try:
FileToSend = open(fileName, "r")
FileContent = FileToSend.read()
connection.sendall(str.encode((FileContent)))
FileToSend.close()
return
except Exception as e:
print("an error occured\n" + str(e))
return
elif (ftype == "bin"):
try:
FileToSend = open(fileName, "rb")
FileContent= FileToSend.read()
connection.sendall((FileContent))
FileToSend.close()
except Exception as e:
print("an error occured\n" + str(e))
return
else:
print("a thingir happened, don't know what")
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
print("socket created successfully")
try:
connection.bind((host, 8080))
print("bind complete")
except socket.error:
print(str(socket.error))
connection.listen(128)
count = 0
while True:
clientConnection, address = connection.accept()
clientConnection.settimeout(60.0*5.0)
print("connected to: " + address[0] + " : " + str(address[1]))
count+=1
print(str(count) + " clients so far")
start_new_thread(threaded_client, (clientConnection,))