Page 1 of 1

Better way for remote console PYTHON

Posted: Thu Feb 04, 2016 12:36 pm
by Kwai chang caine
Hello at all

Since yesterday, i try to remote the console PYTHON without success :oops:
I have try numerous codes, but nothing really good :cry:

With this the stdout works (Thanks to the -i option of PYTHON) but how i can sending command ?

Code: Select all

CheminPython.s = "D:\pyzo2015a\"
NomPython.s = "python.exe"

p = RunProgram(CheminPython + NomPython, "-i -c " + Chr(34) + "print('Hello world!');input('kcc');" + Chr(34), CheminPython,#PB_Program_Open|#PB_Program_Read)
Delay(2000)

While ProgramRunning(p)

 If AvailableProgramOutput(p)
  o$ + ReadProgramString(p) + #CRLF$
  w = ElapsedMilliseconds() + 1000 ; reset timeout
 EndIf
 
 e$ = ReadProgramError(p)
 
 If e$
  o$ + e$ + #CRLF$
  w = ElapsedMilliseconds() + 1000 ; reset timeout
 EndIf
 
 If w < ElapsedMilliseconds()
  Break
  
 EndIf
 
Wend

Debug o$
 
CloseProgram(p)

With this apparently the command not sending correctly to CreateProcess, because the answer is not the same

Code: Select all

; http://www.purebasic.fr/english/viewtopic.php?p=322220#p322220

;process.pb
;Justin 04/10

Structure PROCESS_OBJ
   hReadStdOut.i
   hWriteStdOut.i
   hReadStdErr.i
   hWwriteStdErr.i
   process.PROCESS_INFORMATION
   dataRead.i
   lastErrChar.ASCII
   lastStdChar.ASCII
EndStructure 

#PROCESS_JOIN_OUTPUT = 2 ;stdout and stderr are both redirected to the stdout pipe

Declare.s proc__ReadLine(*lastChar.ASCII, *dataRead, hpipe.i)

;Public functions
Procedure proc_RunProgram(program.s, commad.s, flags.l=0, *err.INTEGER=#Null)
   Define.PROCESS_OBJ *this
   Define.STARTUPINFO start
   Define.SECURITY_ATTRIBUTES sa
   Define.i error
   
   *this = AllocateMemory(SizeOf(PROCESS_OBJ))
   error = 0
   
  ;Create the Pipes
  sa\nLength =SizeOf(SECURITY_ATTRIBUTES)
  sa\bInheritHandle = 1 
  sa\lpSecurityDescriptor = 0 
  
  If CreatePipe_(@*this\hReadStdOut, @*this\hWriteStdOut, @sa, 0) 
     If CreatePipe_(@*this\hReadStdErr, @*this\hWwriteStdErr, @sa, 0) 
        start\cb = SizeOf(STARTUPINFO) 
        start\dwFlags = #STARTF_USESHOWWINDOW | #STARTF_USESTDHANDLES 
        start\hStdOutput = *this\hWriteStdOut 
        If flags & #PROCESS_JOIN_OUTPUT = #PROCESS_JOIN_OUTPUT
           start\hStdError = *this\hWriteStdOut 
        Else
          start\hStdError = *this\hWwriteStdErr 
        EndIf 
        
        If CreateProcess_(0, program + " " + commad, @sa, @sa, 1, #NORMAL_PRIORITY_CLASS, 0, 0, @start, @*this\process) 
           CloseHandle_(*this\hWriteStdOut) 
          CloseHandle_(*this\hWwriteStdErr) 
        
        Else ;CreateProcess error
           error = -3
        EndIf 
     
     Else ;pipe error
        error = -2
     EndIf
  
  Else ;pipe error
     error = -1
  EndIf 
  
  If *err : *err\i = error : EndIf 
   
   If error=0
      ProcedureReturn *this
   
   Else ;error
      CloseHandle_(*this\hReadStdErr) 
    CloseHandle_(*this\hReadStdOut) 
      CloseHandle_(*this\hWriteStdOut) 
    CloseHandle_(*this\hWwriteStdErr) 
      FreeMemory(*this)
      ProcedureReturn #Null
   EndIf
EndProcedure

Procedure proc_CloseProgram(*this.PROCESS_OBJ)
   CloseHandle_(*this\process\hProcess)
   CloseHandle_(*this\process\hThread)
   CloseHandle_(*this\hReadStdErr)
   CloseHandle_(*this\hReadStdOut)
   FreeMemory(*this)
EndProcedure 

Macro proc_ReadStdOutLine(this)
   proc__ReadLine(@this\lastStdChar, @this\dataRead, this\hReadStdOut)
EndMacro

Macro proc_ReadStdErrLine(this)
   proc__ReadLine(@this\lastErrChar, @this\dataRead, this\hReadStdErr)
EndMacro

Procedure proc_GetExitCode(*this.PROCESS_OBJ)
   Define.i exitCode
   
   If GetExitCodeProcess_(*this\process\hProcess, @exitCode)
      ProcedureReturn exitCode
   EndIf 
EndProcedure 

Macro proc_TerminateProcess(this, exitcode)
   TerminateProcess_(this\process\hProcess, exitcode)
EndMacro

Macro proc_DataRead(this)
   this\dataRead
EndMacro

Procedure.s proc__ReadLine(*lastChar.ASCII, *dataRead.INTEGER, hpipe.i)
   Define.ASCII achar
   Define.s text
   
  text = ""
  While ReadFile_(hpipe, @achar, SizeOf(ASCII), *dataRead, 0)
      If achar\a = #CR 
         *lastChar\a = achar\a
         Break
         
      ElseIf achar\a = #LF And *lastChar\a = #CR
         *lastChar\a = achar\a
         Continue
       EndIf
      
      text = text + PeekS(@achar, SizeOf(ASCII), #PB_Ascii)
      *lastChar\a = achar\a
  Wend 
  
  ProcedureReturn text
EndProcedure 

;test
Define.s program, cmd, line, text
Define.PROCESS_OBJ *proc
Define.i prog, counter

CheminPython.s = "D:\pyzo2015a\"
NomPython.s = "python.exe"

*proc = proc_RunProgram(CheminPython + NomPython, "-i -c " + Chr(34) + "print('Hello world!');input(kcc);" + Chr(34), 0)
If *proc   
   counter = 1
   line = proc_ReadStdErrLine(*proc)
   While proc_DataRead(*proc)
      If counter=10
         proc_TerminateProcess(*proc, 100)
      EndIf 
      Debug line

      line = proc_ReadStdErrLine(*proc)

      counter + 1
   Wend
   
   Debug proc_GetExitCode(*proc)
   
   proc_CloseProgram(*proc)
EndIf
And the worst, i can't find the Hwnd with FindWindow_() because in both case no windows appears :shock:

If you have a good way for open PYTHON, read the answer, sending command, and read the answer, etc..
Finaly remote the console

Have a good day

Re: Better way for remote console PYTHON

Posted: Thu Feb 04, 2016 3:30 pm
by IdeasVacuum
.... probably best to un-install Python and erase your mind of it's existence. To continue with it will bring about mass hair loss and an urge to date all of the ladies in the Russian folk group Buranovskiye Babushki. :shock:

Re: Better way for remote console PYTHON

Posted: Thu Feb 04, 2016 5:42 pm
by normeus
@KCC
If this is still about controlling Python to control Selenium then you might be better off using Selenium's java run time

http://selenium-release.storage.googlea ... index.html

download the latest ( currently selenium-server-standalone-2.50.1.jar )

Run it. of course you have to have java installed but you could also download portable java from portablefreeware

Code: Select all

java -jar selenium-server-standalone-#.jar
this will create a server at "http://localhost:4444/wd/hub"

Then using json https://code.google.com/p/selenium/wiki ... reProtocol

you could send commands to it.

To see a sample of commands and more or less formatting of calls look at:

Code: Select all

https://github.com/Element-34/php-webdriver
which is a PHP library but certainly doable in PureBasic.

Norm.

Re: Better way for remote console PYTHON

Posted: Thu Feb 04, 2016 6:53 pm
by Kwai chang caine
@IdeasVacuum
I'm affraid you have surely right :cry:
But it's very difficult now to find new library not OOP...
And when you see they are a splendid lib who do all what you dream, and you can't use it because she is not procedural i'm sad.

I have see also PhamtomJS for do the same job, but i'm sure it's the same problem

At the begining of the week, i believe have found the graal with the embeding of the Python31.dll
http://www.purebasic.fr/english/viewtop ... 35#p481235
A miracle because PYTHON is created in C, so can communicate easily with PB, after PYTHON can use OOP, so my idea is using PYTHON for translate and like this can use the new library

Unfortunately, it's just a little glass :mrgreen:
Because the code of INFRATEC and Epidemicz works, but what i have not thinking it's how adding the library with the DLL :shock:
They are 50 hours i search all solutions in all sense, and not find a simple portable solution :cry:

@normeus
Thanks for your tips. 8)
I don't know this way.
I search portable solution and hate JAVA, but if i can use a portable version, it's perhaps one solution
I take a look, if sometime i have a lightning in the bidet which I use head :mrgreen:

@All
So thanks again at you two for your answer

Re: Better way for remote console PYTHON

Posted: Thu Feb 04, 2016 7:35 pm
by Kwai chang caine
@normeus
I have try what you have say :wink:

Dowload and install
java -jar selenium-server-standalone-2.50.1.jar

Go to the url
http://localhost:4444/wd/hub/static/resource/hub.html

I have an interface with two buttons "Create session" and "Refresh session"
I click to create session
Choose Firefox, because i am in a Firefox browser
And i have this error message "Unable to create new session" :|

I'm not administrator, it's perhaps that :?:

Re: Better way for remote console PYTHON

Posted: Thu Feb 04, 2016 8:47 pm
by normeus
Yes, you have to run Selenium as administrator or be an administrator.
The Selenium website has more information on this.
All this seems really complicated. I didn't read much about Selenium but are you trying to automate webposting? or webdata input?

I cannot make it work with Chrome or Internet Explorer. It works with firefox.
Remember that you'll be runing Selenium.jar using Purebasic.

so you can use Purebasic commands to comunicate:

Code: Select all

;use your own directories for "JAVA" and for "selenium-server-standalone-2.50.1.jar"
  Seleenium = RunProgram("C:\Program Files\Java\jre1.8.0_31\bin\java"," -jar G:\programming\purebasic\selenium-server-standalone-2.50.1.jar","", #PB_Program_Open | #PB_Program_Read|#PB_Program_Write|#PB_Program_Error )
ReadProgramString() 
WriteProgramString()
you comunicate send POST data to "http://localhost:4444/wd/hub/static/resource/hub.html"
GET json response from http://localhost:4444/wd/hub/static/resource/hub.html.


hope that helps.
Norm.

Re: Better way for remote console PYTHON

Posted: Sat Feb 06, 2016 10:44 am
by Kwai chang caine
Thanks a lot for your precious explanations 8)
I try now on another machine with XP in administrator mode :wink:
Have a very good week end

Re: Better way for remote console PYTHON

Posted: Sat Feb 06, 2016 12:11 pm
by Kwai chang caine
Well...i have run selenium with portable JAVA and have this result
XP PRO administrator wrote:C:\JavaPortable>c:\Javaportable\bin\java.exe -jar c:\selenium-server-standalone-
2.50.1.jar
11:53:24.250 INFO - Launching a standalone Selenium Server
11:53:24.484 INFO - Java: Oracle Corporation 25.71-b15
11:53:24.484 INFO - OS: Windows XP 5.1 x86
11:53:24.546 INFO - v2.50.1, with Core v2.50.1. Built from revision d7fc91b
11:53:24.953 INFO - Driver class not found: com.opera.core.systems.OperaDriver
11:53:24.953 INFO - Driver provider com.opera.core.systems.OperaDriver is not re
gistered
11:53:25.000 INFO - Driver provider org.openqa.selenium.safari.SafariDriver regi
stration is skipped:
registration capabilities Capabilities [{browserName=safari, version=, platform=
MAC}] does not match the current platform XP
11:53:25.625 INFO - RemoteWebDriver instances should connect to: http://127.0.0.
1:4444/wd/hub
11:53:25.625 INFO - Selenium Server is up and running
So apparently, it's not also simple :|
I have launched FF, Chrome portable or IE with
http://localhost:4444/wd/hub/static/resource/hub.html
http://127.0.0.1:4444/wd/hub/static/resource/hub.html
http://192.168.1.24localhost:4444/wd/hu ... e/hub.html

Even disabled my antivirus
And have the same result ..a white page :cry:
So it's worst than with W7 in mode no administrator :lol:

In fact, i'm not really surprising because with JAVA it's never really simple.

Thanks when even for your tips :wink: