zy820
V2EX  ›  问与答

我用阿里云 ECS 写个 tcp server 接收 GSM 数据,但是总是有一些国外 IP 连接导致 server 崩了是什么原因,怎么解决

  •  
  •   zy820 · Jul 18, 2018 · 1886 views
    This topic created in 2885 days ago, the information mentioned may be changed or developed.
    8 replies    2018-07-18 19:21:10 +08:00
    mhycy
        1
    mhycy  
       Jul 18, 2018
    代码呢?
    zy820
        2
    zy820  
    OP
       Jul 18, 2018
    @mhycy
    import socketserver

    class MyTCPHandler(socketserver.BaseRequestHandler):

    def handle(self): #所有请求的交互都是在 handle 里执行的,
    while True:
    try:
    self.data = self.request.recv(1024).strip()#每一个请求都会实例化 MyTCPHandler(socketserver.BaseRequestHandler):
    print("{} wrote:".format(self.client_address[0]))
    print(self.data)
    #self.request.sendall(self.data.upper())#sendall 是重复调用 send.
    except ConnectionResetError as e:
    print("err ",e)
    break

    if __name__ == "__main__":
    HOST, PORT = "localhost", 9999 #windows
    #HOST, PORT = "0.0.0.0", 9999 #Linux
    server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) #线程
    server.serve_forever()
    mhycy
        3
    mhycy  
       Jul 18, 2018
    崩掉报什么?
    zy820
        4
    zy820  
    OP
       Jul 18, 2018
    @mhycy 一直
    print("{} wrote:".format(self.client_address[0]))
    print(self.data)
    zy820
        5
    zy820  
    OP
       Jul 18, 2018
    @mhycy client 关闭就一直死循环了
    mhycy
        6
    mhycy  
       Jul 18, 2018
    @zy820
    本来就没有终止条件
    opengps
        7
    opengps  
       Jul 18, 2018
    肯定是那些扫描之类的,发送的数据,你服务器程序识别不了,我做的 gps 接受数据也是遇到过,后来丢弃了所有不合规范的数据包之后好了
    zy820
        8
    zy820  
    OP
       Jul 18, 2018
    @mhycy 可以了,加个 if not self.data:,谢了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1562 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 16:46 · PVG 00:46 · LAX 09:46 · JFK 12:46
    ♥ Do have faith in what you're doing.