0

I am working on a python based web server, but I am receiving this error message. I am still new to python, and not sure what I have done wrong! I am using http://blog.scphillips.com/posts/2012/12/a-simple-python-webserver/ as a reference.

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

Here is my relevant code...

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()
2
  • Please post a Short, Self-contained, Correct Example so that we can run your code and easily fix the problem! Commented Sep 6, 2015 at 4:09
  • No need to apologize :) I'll have a look at your code Commented Sep 6, 2015 at 4:17

1 Answer 1

2

Here's what I think is happening: when you receiving input from the buffer (in req = csock.recv(1024) # get the request, 1kB max), it's coming in the form of bytes. re needs a string. Try the line req = str(req, encoding = 'utf-8') before the line match = re.match('GET /move\?a=(\d+)\sHTTP/1', req), and see how far that gets you. If that doesn't work, try using a different encoding than 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.