简体   繁体   English

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

[英]Regex - how to tell server to ignore unwanted characters

I need your help with my code. 我需要您的代码帮助。 I'm building client + server which will execute my raspi camera MJPG-Stream. 我正在构建客户端+服务器,它将执行我的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()

What is it doing? 到底在做什么 When client sends to the server this: Start640x450x20, server will run the stream with: Width-640 Height-450 Fps-20 当客户端向服务器发送以下内容:Start640x450x20时,服务器将使用以下格式运行流:宽度640高度450 ps 20

All I need is, to execute this only when I put this "Start640x450x20" into client, when I write something like "gfjlshgslsd" I need server to ignore it, not to turn down. 我需要做的就是仅在将“ Start640x450x20”放入客户端时执行此操作,当我编写“ gfjlshgslsd”之类的内容时,我需要服务器忽略它,而不是拒绝它。

Thanks for help. 感谢帮助。

The entire code is correct. 整个代码是正确的。 You have just missed 1 check. 您刚错过了1张支票。

Just check if m is None or not before calling m.groups() 只需在调用m.groups()之前检查m是否为None m.groups()

if m:
    hodnoty = m.groups()
    for cislo in hodnoty: 
        print (cislo)

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

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