pythoniosxcodesimplehttpserverrun-script

Run a python HttpServer before run a iOS project in Xcode


I’m trying to run a http server before run an iOS project. I've added a script python -m SimpleHTTPServer 9527 in Run Script. enter image description here It's OK when I run it in Terminal, But when I clicked the Run button in Xcode, it stoped like

enter image description here

When I stoped the project,Xcode showed some error log:

Serving HTTP on 0.0.0.0 port 9527 ... Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main "main", fname, loader, pkg_name) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleHTTPServer.py", line 235, in test() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleHTTPServer.py", line 231, in test BaseHTTPServer.test(HandlerClass, ServerClass) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 599, in test httpd.serve_forever() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 236, in serve_forever poll_interval) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 155, in _eintr_retry return func(*args) KeyboardInterrupt

Please help or try to give some ideas how to achieve this. Thanks in advance.


Solution

  • Your build is remaining within this phase because the command you are executing will not terminate until you request it to. Most likely, the web server is running correctly, however Xcode is waiting for it to terminate, which it won't do until you click the 'stop' button.

    The stack trace which you see is normal when you stop the inbuilt web server within Python. You'll see a similar message if you use CTRL + C to exit it when using Terminal.

    Generally, the 'execute shell' phases of your build should run commands which perform a task and then exit by themselves, however if you're trying to make the web server run for the duration of the build, you'd need to employ two phases. One phase at the beginning of the build to start the web server and fork it and another phase at the end of the build to terminate it. Whilst variables don't persist between built phases from my experience, you could write the PID to a file and then read it within the later stage.

    Aside from this solution, I'd suggest rethinking if you need to run a server as part of your build. If you start to use some from of Continuous Integration in the future, or just want to run multiple builds at the same time, additional complexities of allocating port numbers will introduce a challenge.