如果没有收到SSL消息,如何避免在python套接字服务器上崩溃

时间:2017-08-07 11:04:05

标签: python sockets ssl

我已经使用python ssl.socket对服务器进行了编码,每当收到非SSL的消息时,服务器就会出现以下错误:

  

追踪(最近一次通话):   文件" ./ server.py",第88行,in      密钥文件=" server.key&#34)    文件" /usr/lib64/python2.7/ssl.py",第936行,在wrap_socket中      密码=密码)    文件" /usr/lib64/python2.7/ssl.py",第611行, init      self.do_handshake()    文件" /usr/lib64/python2.7/ssl.py",第833行,在do_handshake中      self._sslobj.do_handshake()    ssl.SSLEOFError:EOF违反协议(_ssl.c:579)

这是服务器代码:

            #!/usr/bin/python
            import socket
            import ssl
            import sys
            import os
            import MySQLdb
            import logging

            from function import encrypt

            # Create a TCP/IP socket
            bindsocket = socket.socket()
            bindsocket.bind(('', 10023))
            bindsocket.listen(5)

            # Bind the socket to the port
            print >>sys.stderr, 'starting up on port %s' % bindsocket.bind
            def process_data(connstream, data):
                data1 = data.rstrip()
                data_list = data1.split(",")
                print >>sys.stderr, 'received "%s"' % data_list
                if data1:
                    print >>sys.stderr, 'Checking Database'
                    con = MySQLdb.connect('localhost', 'XXXX', 'XXXX', 'XXXX')
                    cursor = con.cursor()
                    cursor.execute("SELECT * FROM hostname WHERE systemname = '%s'" % data_list[1])
                    rowsz = cursor.rowcount
                    result = cursor.fetchall()
                    if data_list[0] == "server":
                        if rowsz == 1:
                            print >>sys.stderr, 'Updating Hostname:"%s"' % data_list[1]
                            cursor.execute("UPDATE hostname set systemname='%s',domain='%s',vendor='%s',model='%s',machinetype='%s',os='%s',osrelease='%s',machineserial='%s' where systemname = '%s'" % (data_list[1], data_list[2], data_list[3], data_list[4], data_list[5], data_list[6], data_list[7], data_list[8], data_list[1]))
                            cursor.execute("SELECT id FROM hostname where systemname = '%s'" % data_list[1])
                            detids = cursor.fetchone()
                            print >>sys.stderr, 'Updating datacenter for ID: "%s"' % detids[0]
                            cursor.execute("UPDATE details set datacenter='%s' where detid = '%s'"  % (data_list[9], detids[0]))
                        elif rowsz < 1:
                            print >>sys.stderr, 'Inserting Hostname:"%s"' % data_list[1]
                            cursor.execute("INSERT INTO hostname (systemname, domain, vendor, model, machinetype, os, osrelease, machineserial) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (data_list[1], data_list[2], data_list[3], data_list[4], data_list[5], data_list[6], data_list[7], data_list[8]))
                            default= 'default'
                            defaultint = 1
                            cursor.execute("SELECT id FROM hostname where systemname = '%s'" % data_list[1])
                            detids = cursor.fetchone()
                            print >>sys.stderr, 'Updating details for id: "%s"' % detids[0]
                            cursor.execute("INSERT INTO details (detid, inservice, globalstatus, managedby, machineconsole, passwd, application, datacenter, rackspace, mlog) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (detids[0], defaultint, defaultint, default, default, default, default, default, default, default))
                        else:
                            print >>sys.stderr, 'Nothing updated'
                    elif data_list[0] == "network":
                        cursor.execute("SELECT id FROM hostname where systemname = '%s'" % data_list[1])
                        detids = cursor.fetchone()
                        cursor.execute("SELECT * from network where netid = '%s'" % detids[0])
                        rowszz = cursor.rowcount
                        print >>sys.stderr, 'Count : "%s"' % rowszz
                        if rowszz >= 1:
                            print >>sys.stderr, 'Updating network details for id: "%s"' % detids[0]
                            for i in xrange(2, len(data_list), 5):
                                if data_list[i]:
                                    sqlcmd = "UPDATE network set netid='{val1}', interface='{val2}', ipaddress='{val3}', macaddress='{val4}', gateway='{val5}', interfacestatus='{val6}' WHERE interface = '{val2}' AND netid = '{val1}'".format(val1=detids[0], val2=data_list[i], val3=data_list[i+1], val4=data_list[i+2], val5=data_list[i+3], val6=data_list[i+4])
                                    cursor.execute(sqlcmd)
                                else:
                                    print >>sys.stderr, 'Inserting network details for id: "%s"' % detids[0]
                                    for i in xrange(2, len(data_list), 5):
                                        if data_list[i]:
                                            sqlcmd = "INSERT INTO network (netid, interface, ipaddress, macaddress, gateway, interfacestatus) VALUES ('{val1}', '{val2}', '{val3}', '{val4}', '{val5}', '{val6}')".format(val1=detids[0], val2=data_list[i], val3=data_list[i+1], val4=data_list[i+2], val5=data_list[i+3], val6=data_list[i+4])
                                            cursor.execute(sqlcmd)
                    elif data_list[0] == "rootpwd":
                        print >>sys.stderr, 'Updating root pwd for :"%s"' % data_list[1]
                        cursor.execute("SELECT id FROM hostname where systemname = '%s'" % data_list[1])
                        detsids = cursor.fetchone()
                        ciphertext = encrypt(data_list[2])
                        cursor.execute("UPDATE details set passwd='%s' where detid = '%s'" % (ciphertext, detsids[0]))
                else:
                    print >>sys.stderr, 'no more data from', client_address
                return False

            def deal_with_client(connstream):
                 break
            data = connstream.read()

while True:
    newsocket, fromaddr = bindsocket.accept()
    connstream = ssl.wrap_socket(newsocket,
                                 server_side=True,
                                 certfile="server.crt",
                                 keyfile="server.key")
    try:
        deal_with_client(connstream)

    finally:
        connstream.shutdown(socket.SHUT_RDWR)
        connstream.close()

并且客户端正在向服务器发送信息,如下所示:

  

/ bin / echo $ variable,$ variable,$ variable | / usr / bin / openssl s_client -quiet -connect hostname:port

如果收到的邮件对服务器没有意义或邮件收到的格式错误,有没有办法避免服务器崩溃?

谢谢。

0 个答案:

没有答案