1. 程式人生 > >Mac搭建本地伺服器及測試demo

Mac搭建本地伺服器及測試demo

1.伺服器端搭建
# brew install node
# brew install nginx

# nginx -t //獲取nginx配置檔案所在目錄
# emacs /usr/local/etc/nginx/nginx.conf //修改nginx配置檔案
events {
    worker_connections  51200;
}

http {
    server {
    	   root /Users/name/working/www; //www為我們建立的目錄,下面為led.json檔案。
    }
}

# mkdir -p /Users/name/working/www
# emacs /Users/name/working/www/led.json
{
    "led":true
}
# sudo nginx -c /usr/local/etc/nginx/nginx.conf
# sudo nginx -s reload //配置完成後,重啟nginx服務

2.測試
在瀏覽器中輸入:127.0.0.1/led.json 進行測試。


3.客戶端python訪問伺服器
test.py
import urllib2,json
results = urllib2.urlopen('http://192.168.31.143/led.json').read()
response = json.loads(results)['led']
print (response)