Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python多线程服务器-客户端-服务器通信问题_Python_Html_Multithreading_Server - Fatal编程技术网

Python多线程服务器-客户端-服务器通信问题

Python多线程服务器-客户端-服务器通信问题,python,html,multithreading,server,Python,Html,Multithreading,Server,我正在尝试构建一个简单的多线程服务器,当通过浏览器查询时,该服务器会回复请求的html页面或404页面 这是我的密码: from socket import * import thread def handler(connectionSocket,addr): try: message = connectionSocket.recv(2048) filename = message.split()[1] f = open(filena

我正在尝试构建一个简单的多线程服务器,当通过浏览器查询时,该服务器会回复请求的html页面或404页面

这是我的密码:

from socket import *
import thread

def handler(connectionSocket,addr):
    try:
        message = connectionSocket.recv(2048) 
        filename = message.split()[1] 
        f = open(filename[1:], 'r')
        outputdata = f.read()

        #Send one HTTP header line into socket
        connectionSocket.send("\nHTTP/1.0 200 OK\n\n")

        #Send the content of the requested file to the client
        connectionSocket.send(outputdata)

        #Close client socket
        f.close()
        connectionSocket.close()
    except IOError:
        #Send response message for file not found
        print "404 Not Found"

        #Send over 404 page
        f = open("404.php",'r')
        outputdata = f.read()
        connectionSocket.send("\nHTTP/1.0 404 Not Found\n\n")
        connectionSocket.send(outputdata)

        #Close client socket
        f.close()
        connectionSocket.close()


def main():
    #Prepare a sever socket
    serverPort = 6789
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.bind(('',serverPort))
    serverSocket.listen(1)
    print "Starting server on port: " + str(serverPort) + ". User <CTRL-C> to stop"

    while True:
        #Establish the connection
        print 'Ready to serve...'
        connectionSocket, addr = serverSocket.accept()
        print '...connected from:', addr
        thread.start_new_thread(handler,(connectionSocket,addr))

    serverSocket.close()

if __name__ == '__main__':
    main()
从套接字导入*
导入线程
def处理器(连接插座,地址):
尝试:
message=connectionSocket.recv(2048)
filename=message.split()[1]
f=打开(文件名[1:],'r')
outputdata=f.read()
#将一个HTTP头行发送到套接字
connectionSocket.send(“\nHTTP/1.0 200正常\n\n”)
#将请求文件的内容发送到客户端
connectionSocket.send(输出数据)
#关闭客户端套接字
f、 关闭()
connectionSocket.close()连接
除IOError外:
#未找到文件的发送响应消息
打印“404未找到”
#发送404页以上
f=open(“404.php”,“r”)
outputdata=f.read()
connectionSocket.send(“\nHTTP/1.0 404未找到\n\n”)
connectionSocket.send(输出数据)
#关闭客户端套接字
f、 关闭()
connectionSocket.close()连接
def main():
#准备一个服务器插座
服务器端口=6789
serverSocket=socket(AF\u INET,SOCK\u STREAM)
serverSocket.bind(“”,serverPort))
serverSocket.listen(1)
打印“在端口上启动服务器:”+str(serverPort)+“。用户停止”
尽管如此:
#建立连接
打印“准备好服务…”
connectionSocket,addr=serverSocket.accept()
打印“…连接自:”,地址
线程。启动新线程(处理程序(connectionSocket,addr))
serverSocket.close()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
当我查询helloworld.html时,我收到了该网页,但服务器似乎连接了4次,如下所示。打开页面后,它会在“准备好服务…”状态下停留几分钟,然后出现错误

Starting server on port: 6789. User <CTRL-C> to stop
Ready to serve...
...connected from: ('127.0.0.1', 61382)
Ready to serve...
...connected from: ('127.0.0.1', 61386)
Ready to serve...
...connected from: ('127.0.0.1', 61387)
Ready to serve...
...connected from: ('127.0.0.1', 61388)
Ready to serve...
Unhandled exception in thread started by <function handler at 0x10051a410>
Traceback (most recent call last):
 File "web_server2.py", line 13, in handler
    filename = message.split()[1]
IndexError: list index out of rangeUnhandled exception in thread started by <function handler at 0x10051a410>
Traceback (most recent call last):
  File "web_server2.py", line 13, in handler
    filename = message.split()[1]
IndexError: list index out of range

Unhandled exception in thread started by <function handler at 0x10051a410>
Traceback (most recent call last):
  File "web_server2.py", line 13, in handler
    filename = message.split()[1]
IndexError: list index out of range
^CTraceback (most recent call last):
  File "web_server2.py", line 58, in <module>
    main()
  File "web_server2.py", line 51, in main
    connectionSocket, addr = serverSocket.accept()
  File "//anaconda/lib/python2.7/socket.py", line 206, in accept
    sock, addr = self._sock.accept()
正在端口6789上启动服务器。用户停止
准备好服务了。。。
…连接自:('127.0.0.1',61382)
准备好服务了。。。
…连接自:('127.0.0.1',61386)
准备好服务了。。。
…连接自:('127.0.0.1',61387)
准备好服务了。。。
…连接自:('127.0.0.1',61388)
准备好服务了。。。
由启动的线程中存在未处理的异常
回溯(最近一次呼叫最后一次):
处理程序中第13行的文件“web_server2.py”
filename=message.split()[1]
索引器错误:列表索引超出范围由启动的线程中未处理的异常
回溯(最近一次呼叫最后一次):
处理程序中第13行的文件“web_server2.py”
filename=message.split()[1]
索引器:列表索引超出范围
由启动的线程中存在未处理的异常
回溯(最近一次呼叫最后一次):
处理程序中第13行的文件“web_server2.py”
filename=message.split()[1]
索引器:列表索引超出范围
^CTraceback(最近一次通话最后一次):
文件“web_server2.py”,第58行,在
main()
文件“web_server2.py”,第51行,主
connectionSocket,addr=serverSocket.accept()
文件“//anaconda/lib/python2.7/socket.py”,第206行,接受
sock,addr=self.\u sock.accept()
从错误回溯来看,程序似乎连接并发送了页面,然后认为它已再次连接,但当未发送任何查询时,数组为空,它将带边界错误退出

类似地,当我查询不在那里的页面并收到404页面时:

Starting server on port: 6789. User <CTRL-C> to stop
Ready to serve...
...connected from: ('127.0.0.1', 55032)
Ready to serve...
...connected from: ('127.0.0.1', 55033)
Ready to serve...
404 Not Found
...connected from: ('127.0.0.1', 55059)
Ready to serve...
...connected from: ('127.0.0.1', 55061)
Ready to serve...
...connected from: ('127.0.0.1', 55063)
Ready to serve...
...connected from: ('127.0.0.1', 55065)
Ready to serve...
...connected from: ('127.0.0.1', 55067)
Ready to serve...
...connected from: ('127.0.0.1', 55069)
Ready to serve...
...connected from: ('127.0.0.1', 55071)
Ready to serve...
...connected from: ('127.0.0.1', 55073)
Ready to serve...
...connected from: ('127.0.0.1', 55075)
Ready to serve...
...connected from: ('127.0.0.1', 55077)
Ready to serve...
...connected from: ('127.0.0.1', 55079)
Ready to serve...
...connected from: ('127.0.0.1', 55093)
Ready to serve...
404 Not Found
...connected from: ('127.0.0.1', 55095)
Ready to serve...
404 Not Found
正在端口6789上启动服务器。用户停止
准备好服务了。。。
…连接自:('127.0.0.1',55032)
准备好服务了。。。
…连接自:('127.0.0.1',55033)
准备好服务了。。。
404找不到
…连接自:('127.0.0.1',55059)
准备好服务了。。。
…连接自:('127.0.0.1',55061)
准备好服务了。。。
…连接自:('127.0.0.1',55063)
准备好服务了。。。
…连接自:('127.0.0.1',55065)
准备好服务了。。。
…连接自:('127.0.0.1',55067)
准备好服务了。。。
…连接自:('127.0.0.1',55069)
准备好服务了。。。
…连接自:('127.0.0.1',55071)
准备好服务了。。。
…连接自:('127.0.0.1',55073)
准备好服务了。。。
…连接自:('127.0.0.1',55075)
准备好服务了。。。
…连接自:('127.0.0.1',55077)
准备好服务了。。。
…连接自:('127.0.0.1',55079)
准备好服务了。。。
…连接自:('127.0.0.1',55093)
准备好服务了。。。
404找不到
…连接自:('127.0.0.1',55095)
准备好服务了。。。
404找不到
它连接多次,但只通过ioexception部分3次,因为只有3个“404未找到”打印。但是现在,服务器并不像helloworld.html页面那样退出,它保持与上面的一样,当用一个不存在的页面再次查询时,我得到另一个404。但是当查询helloworld.html时,它会显示它,然后退出,错误与上面相同


任何关于在哪里查找bug的帮助都将非常有用。我可以从测试中看出,这似乎与我如何处理I/o有关,但我不确定问题出在哪里。这是我的第一个服务器程序,希望了解更多。谢谢

尝试在拆分之前打印
消息
。不相关:您不应该在http头之前发送换行符…@CristiFati当我在拆分消息之前打印消息时,我得到三个连接,然后是打印的消息(get/helloworld.html http/1.1…等),然后是两个连接(即“…连接自:”)然后是具有相同边界错误的退出。因此,它似乎在实际打印消息之前,已点击“打印”…连接自:“,addr”三次。“在一次连接中正确执行后,它再次命中同一行。”mata指出。我会解决的,谢谢。我记得(希望我不是在说废话),为了避免MITM攻击,较新的浏览器(从今年年初开始)将请求(至少在加密通道上)数据分两部分发送(还包括一个G字符),在一些旧的HTTP服务器上生成HTTP 400响应(必须更新)。你可以