简体   繁体   English

Python中的简单套接字编程:糟糕的服务器

[英]Simple Socket Programming in Python: Bad Servers

I am trying to write a simple text/HTTP server which in python to simulate a variety of bad network conditions. 我正在尝试编写一个简单的文本/ HTTP服务器,它在python中模拟各种糟糕的网络条件。 In the first place, I want a server which will appear to close the connection without sending the browser any data in order to reliably create the "net::ERR_EMPTY_RESPONSE" browser error. 首先,我想要一个服务器,它似乎关闭连接而不向浏览器发送任何数据,以便可靠地创建“net :: ERR_EMPTY_RESPONSE”浏览器错误。 Most tutorials about socket programming deal with how to create good servers, not how to create bad servers so I'm sure you can appreciate my problem here. 大多数关于套接字编程的教程都涉及如何创建好的服务器,而不是如何创建糟糕的服务器,所以我相信你可以在这里欣赏我的问题。

Here's what I have so far: 这是我到目前为止所拥有的:

    import socket, sys, time

    port = 8080
    print "Bad server is listening on " + str(port)
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    serversocket.bind(('localhost', port))

    serversocket.listen(5)

    #simulate successful transport after the 5th attempt
    counter = 0

    while True:
        #accept connections from outside
        (clientsocket, address) = serversocket.accept()
        time.sleep(1);

        if counter > 5:
            serversocket.write('<span class="position: relative; left: 20px;>hello</span>');

        serversocket.close()

        counter += 1

The counter is because I thought it might be nice if the server replies after the 5th connection attempt in order to simulate a very unreliable network connection. 计数器是因为我认为如果服务器在第5次连接尝试后回复以模拟非常不可靠的网络连接可能会很好。

However, despite being a bad server, the bad server should not itself crash. 然而,尽管服务器是坏的,坏服务器本身不应该崩溃。 When I run that code I get: 当我运行该代码时,我得到:

    Bad server is listening on 8080
    Traceback (most recent call last):
      File "bad_server.py", line 16, in <module>
        (clientsocket, address) = serversocket.accept()
      File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 195, in accept
        sock, addr = self._sock.accept()
      File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 165, in _dummy
        raise error(EBADF, 'Bad file descriptor')
    socket.error: [Errno 9] Bad file descriptor

This is not desirable. 这是不可取的。 The bad server should carry on listening for connections. 坏服务器应该继续监听连接。

My next task after getting this working is to try to make a bad server abruptly terminates its reply, resulting in the net::ERR_ABORTED in a browser. 完成此工作后,我的下一个任务是尝试使错误的服务器突然终止其回复,从而在浏览器中生成net :: ERR_ABORTED。 I have no idea how I'll go about that though. 我不知道我怎么会这样做。 Any help would be appreciated. 任何帮助,将不胜感激。

You're supposed to use the clientsocket to talk to the client; 你应该使用clientsocket与客户交谈; serversocket just listens. serversocket只是听。

while True:
    #accept connections from outside
    (clientsocket, address) = serversocket.accept()
    time.sleep(1);

    if counter > 5:
        clientsocket.write('<span class="position: relative; left: 20px;>hello</span>');

    clientsocket.close()

    counter += 1

Also, you don't reset counter , so every connection after the fifth will "succeed". 此外,您不重置counter ,因此第五个之后的每个连接都将“成功”。 Hope this is desired. 希望这是理想的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM