Python 3.x Python3:如何在后台运行localhost?

Python 3.x Python3:如何在后台运行localhost?,python-3.x,python-requests,Python 3.x,Python Requests,这是我的代码: import _thread import http.server import socketserver def func(): PORT = 8002 Handler = http.server.SimpleHTTPRequestHandler httpd = socketserver.TCPServer(("", PORT),Handler) _thread.start_new_thread(httpd.serve_forever(

这是我的代码:

import _thread 
import http.server
import socketserver

def func():
    PORT = 8002
    Handler = http.server.SimpleHTTPRequestHandler

    httpd = socketserver.TCPServer(("", PORT),Handler)


    _thread.start_new_thread(httpd.serve_forever())

func()
我想在后台运行,是否可能。上面的代码没有按预期工作,它在前台工作。如何在后台运行

我想用它来测试用python编写的下载程序

.....

r=requests.get('http://localhost:8002/file.dat',stream=True)

with open('file.dat','wb')as f:
    for chunk in r.iter_content(chunk_size=(1*1024*1024):
        f.write(chunk)

......

我不能使用多个处理器,因为我正在用Python编写iOS中的代码,它不支持使用多个进程,但支持使用多线程?如果HTTP服务器在您的iDevice上运行,您确定应用程序没有挂起吗?@hsk I的意思是它正在工作,但不是在另一个线程上,而是在主线程上工作