bpy.context.screen is None for a script running with threads

I’m started to control blender (2.62) using a little webserver script which uses threads and sending commands to it from a browser. It’s running fine for changing object locations.
I now want to save a current screenshot to a file using:
bpy.ops.screen.screenshot(filepath=“c:\ emp\ est8.bmp”)
I get the following exception:

Exception happened during processing of request from (‘127.0.0.1’, 14230)
Traceback (most recent call last):
File “C:\Program Files\Blender Foundation\Blender\2.62\python\lib\socketserver
.py”, line 284, in _handle_request_noblock
self.process_request(request, client_address)
File “C:\Program Files\Blender Foundation\Blender\2.62\python\lib\socketserver
.py”, line 310, in process_request
self.finish_request(request, client_address)
File “C:\Program Files\Blender Foundation\Blender\2.62\python\lib\socketserver
.py”, line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File “C:\Program Files\Blender Foundation\Blender\2.62\python\lib\socketserver
.py”, line 639, in init
self.handle()
File “C:\Program Files\Blender Foundation\Blender\2.62\python\lib\http\server.
py”, line 395, in handle
self.handle_one_request()
File “C:\Program Files\Blender Foundation\Blender\2.62\python\lib\http\server.
py”, line 383, in handle_one_request
method()
File “C:\Users\Irgenfs\Documents\Projekte\Cadavision\svn runk\blender\sockets
.blend\Text.001”, line 30, in do_GET
File “C:\Program Files\Blender Foundation\Blender\2.62\scripts\modules\bpy\ops
.py”, line 180, in call
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.screen.screenshot.poll() failed, context is incor
rect

It work’s fine running it on it’s own from the text editor:


bpy.context.screen

Here’s my complete scipt:


import threading
import http.server
import socketserver
from urllib.parse import urlparse
import time
import bpy

PORT = 8000
IP_ADDRESS = "localhost"
#IP_ADDRESS = "192.168.254.1"
global running
running = 1


class RequestHandler(http.server.BaseHTTPRequestHandler):
    def _writeheaders(self):
      self.send_response(200)
      self.send_header('Content-type', 'text/html; charset=UTF-8')
      self.end_headers()
    
    def do_HEAD(self):
      self._writeheaders()
        
    def do_GET(self):
      global running  
      parsed_path = urlparse(self.path)
      #ctx = bpy.context.screen
      #print(ctx)
      #Here the context is None
      bpy.ops.screen.screenshot(filepath="c:\	emp\	est8.bmp")
      try:
        params = dict([p.split('=') for p in parsed_path[4].split('&')])
      except:
        params = {}
      print(params)    
      try:
        running = int(params["running"])
      except:
        running = 1
      try:
        print("Here i am")
        #Here it crashes!
        #bpy.ops.render.render(write_still=True)
      except:
        print("no xrot")      
      self._writeheaders()
      retstr = """<HTML>
<HEAD><TITLE>Dummy response</TITLE></HEAD>
<BODY>dummy response</BODY>
</HTML>"""
      self.wfile.write(retstr.encode('latin'))
      print("running = ",running) 

    
httpd = socketserver.TCPServer((IP_ADDRESS, PORT), RequestHandler)



class HttpThread(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
  def run(self):
    print("HTTP Server THREAD: started")
    print("serving at port", PORT)
    httpd.serve_forever()
    print ("HTTP Server THREAD: finished")
      
class ControlThread(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
  def run(self):
    print("Control THREAD: started")
    runcontrol = 1    
    while runcontrol >0:
      if running < 1:
        print("try server shutdown")
        httpd.shutdown()
        httpd.socket.close() 
        print("shutdown finished")
        runcontrol = 0
      time.sleep(1)                
        
    print ("Control THREAD: finished")
            
print ("SCRIPT: started")    
http_thread = HttpThread()
http_thread.start()

control_thread = ControlThread()
control_thread.start()
print ("SCRIPT: finished") 

To test, just enter http://localhost:8000 into browser address bar

Any idea how to get around this problem?

Thanks,
Stephan

For command line operations and rendering do not use the context or bpy.ops. You will have to write your own code for those operations. bpy.data is available all the time.