linux - Python 服务器在 Linux 中不工作

标签 linux python-3.x sockets linux-mint

不久前,我在一台 windows 7 机器上用 python 3.6 写了一个服务器,它在那里工作。我已经将服务器转移到带有 Cinnamon 桌面的 linux mint 18.3 Sylvia。出于某种原因,该程序不接受连接,即使将端口从 80 更改为 8080 也是如此。我通过在另一台机器上使用 Internet Explorer 来诊断网络问题来确定这一点(是的,我确实在 IP 地址之后放置了 :8080)。我的问题是:为了使程序在 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,))

最佳答案

我没有足够的积分来评论,所以:

确保您的端口在 Linux 设备上已打开。首先通过 sudo ufw status 检查您是否启动并运行了防火墙。如果启动,允许您通过 sudo ufw allow 8080sudo ufw allow 80 访问的端口。

一旦您的端口打开并可访问,就没有理由不运行。

希望对您有所帮助。

关于linux - Python 服务器在 Linux 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50161059/

相关文章:

linux - 在 linux 中使用 verbose 启动 tomcat

python-3.x - PyQt5 - 如何在鼠标点击位置画一个点?

PostgreSQL 数据库中的 Linux 系统利用率导入

java - Linux 关机和 Java 关机 Hook

django - django-bootstrap4 和 bootstrap4 有什么区别?

python - 按键排序字典,然后按值排序(列表或元组?)

python - 如何处理线程模块中引发的 KeyboardException?

c# - 为什么我的表单在调用 BeginConnect 时卡住?

sockets - 如何在 Go 中停止监听服务器

linux - 如何使用 grep(或其他 LSB 工具)计算 .po 中的空翻译?