2015-09-06 113 views
0

我正在使用基於python的web服務器,但我收到此錯誤消息。我對Python仍然很陌生,不確定我做錯了什麼!我使用http://blog.scphillips.com/posts/2012/12/a-simple-python-webserver/作爲參考。Python網絡服務器錯誤

Traceback (most recent call last): File "control.py", line 203, in <module> 
    app = TetheredDriveApp() File "control.py", line 77, in __init__ 
    match = re.match('GET /api\?action=(\d|[A-Z]+)\sHTTP/1', req) File "/usr/lib/python3.2/re.py", line 156, in match 
    return _compile(pattern, flags).match(string) TypeError: can't use a string pattern on a bytes-like object 

這裏是我的相關代碼...

import socket 
import re 

def __init__(self): 
    host = '' 
    port = 61338 
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    sock.bind((host, port)) 
    sock.listen(1) 

    self.onConnect() 

    while True: 
     csock, caddr = sock.accept() 
     req = csock.recv(1024) 
     match = re.match('GET /api\?action=(\d|[A-Z]+)\sHTTP/1', req) 
     motionChange = False 
     if match: 
      action = match.group(1) 

      # KeyPress 
      if action == 'PP': 
      self.sendCommandASCII('128') 

     else: 
      csock.sendall("0") 
     csock.close() 
+0

請發佈一個[簡短,獨立,正確的示例](http://sscce.org/),以便我們可以運行您的代碼並輕鬆解決問題! –

+0

不需要道歉:)我會看看你的代碼 –

回答

2

這就是我認爲正在發生的事情:當你從緩衝區接收輸入(在req = csock.recv(1024) # get the request, 1kB max),它以字節爲單位的形式到來。 re需要一個字符串。嘗試match = re.match('GET /move\?a=(\d+)\sHTTP/1', req)之前的行req = str(req, encoding = 'utf-8'),然後查看您得到了多少。如果這不起作用,請嘗試使用與utf-8不同的編碼。

req = csock.recv(1024) 
req = str(req, encoding='utf-8') # encoding may need to be changed 
match = re.match('GET /api\?action=(\d|[A-Z]+)\sHTTP/1', req)