正则表达式 - 如何告诉服务器忽略不需要的字符

时间:2017-06-23 09:27:32

标签: python regex server client

我的代码需要你的帮助。我正在构建客户端+服务器,它将执行我的raspi相机MJPG-Stream。

import socket
import subprocess
import re

comms_socket = socket.socket()
comms_socket.bind(('', 4244))
comms_socket.listen(20)


reg_vyraz = "Start(\d+)x(\d+)x(\d+)"


try:
 while True:
    print("Waiting for connection... (Ctrl+C to exit)")
    connection, adress = comms_socket.accept()

    print("Connected")
    try:
        while True:
            received = connection.recv(4096).decode('UTF-8') 
            if (len(received) == 0):
                break;

            m = re.match(reg_vyraz, received)

            hodnoty = m.groups()

            for cislo in hodnoty: 
                    print (cislo)                   

            print ('Received ', hodnoty, ' from the client')

        sys.stdout.write("\n")
       except KeyboardInterrupt:
        print("Closing connection")
        connection.close()


except KeyboardInterrupt:
 print("Closing server")
 comms_socket.close()

它在做什么?当客户端发送到服务器时:Start640x450x20,服务器将运行以下流: 宽度640 身高-450 FPS-20

所有我需要的是,只有当我将“Start640x450x20”放入客户端时执行此操作,当我写“gfjlshgslsd”时,我需要服务器忽略它,而不是拒绝。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

整个代码都是正确的。你错过了1次检查。

在调用serializer.save()

之前,只需检查m是否为None
m.groups()