Можно ли отключить сеть в iOS Simulator?

Вы не можете запускать .py файлы из JavaScript без программы Python, так как вы не можете открывать файлы .txt без текстового редактора. Но все это становится дыханием с помощью Web API Server (IIS в примере ниже).

  1. Установите python и создайте файл-образец test.py
    import sys
    # print sys.argv[0] prints test.py
    # print sys.argv[1] prints your_var_1
    
    def hello():
        print "Hi" + " " + sys.argv[1]
    
    if __name__ == "__main__":
        hello()
    
  2. Создайте метод на вашем веб-сервере API
    [HttpGet]
    public string SayHi(string id)
    {
        string fileName = HostingEnvironment.MapPath("~/Pyphon") + "\\" + "test.py";          
    
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(@"C:\Python27\python.exe", fileName + " " + id)
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        p.Start();
    
        return p.StandardOutput.ReadToEnd();                  
    }
    
  3. И теперь для вашего JavaScript:
    function processSayingHi() {          
       var your_param = 'abc';
       $.ajax({
           url: '/api/your_controller_name/SayHi/' + your_param,
           type: 'GET',
           success: function (response) {
               console.log(response);
           },
           error: function (error) {
               console.log(error);
           }
        });
    }
    

Помните, что ваш .py-файл не будет запускается на компьютере пользователя, но вместо этого на сервере.

386
задан Paul Hammond 26 January 2011 в 18:30
поделиться