PopenSpawn and ftp command - python

question - i am trying to make minimal example with latest pexpect,
but cant get to working basic example(src is below) in following
enviroment:
windows 10, 64bit python 3.4
command is ftp localhost ( server is filezila, running on localhost)
i am aware windows support in pexpect, is marked as experimental, but still, it would be usefull to get it working ..
problem:
if in code below i use
co.expect("",timeout=30)
, then scripts works, because it is not waiting for prompt after login... but as i need to interact and put more complex query, i need to use
co.expect("ftp>",timeout=30)
but at that moment, pexpect wait till timeout.. i found, in popen_spawn.py, nothing is coming. is it possible self.proc.stdout.fileno() is buffering, and waiting indefinetly, till buffer is filled ?
import pexpect
from pexpect import popen_spawn
import sys
try:
hostname = "127.0.0.1"
co = pexpect.popen_spawn.PopenSpawn('ftp localhost',encoding="utf-8")
co.logfile = sys.stdout
co.timeout = 4
co.expect(":")
co.sendline("test")
co.expect(".*word:.*")
co.sendline("test123")
co.expect("",timeout=30)
co.sendline('dir')
co.expect('ftp>')
co.close()
except Exception as e:
print(co)

Related

When using text() with python-escpos I get [Errno None] and key error = 1 (windows 10)

I am trying to print to a TM-T20II thermal printer so I can print receipts. Here is my code:
from escpos import printer
from escpos import *
import escpos
from escpos import config
import usb.core
import usb.util
import usb.backend.libusb1
from ctypes import c_void_p, c_int
backend = usb.backend.libusb1.get_backend(find_library=lambda x: "libusb-1.0.dll")
backend.lib.libusb_set_option.argtypes = [c_void_p, c_int]
backend.lib.libusb_set_option(backend.ctx, 1)
p = printer.Usb(0x04b8,0x0e15,0,0x82,0x01, backend=backend)
p.text('test')
I am using a usbdk backend, without it I get a 'NotImplementedError: Operation not supported or unimplemented on this platform.' I'm doing this because for my program to work I need to use the default Epson drivers. When I run this code the error I get is:
<File "C:\Users\maxsl\anaconda3\lib\site-packages\usb\core.py", line
234, in get_interface_and_endpoint
return self._ep_info[endpoint_address]
KeyError: 1
During handling of the above exception, another exception occurred:
File
"C:\Users\maxsl\anaconda3\lib\site-packages\usb\backend\libusb1.py",
line 604, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
USBError: [Errno None] Other error>
This error only occurs when I add p.text() in. Finding the printer and everything else is no problem. I also want to say that write() works in the PyUSB module, but it would be much more convenient for me to not have to translate the outputs in my program to the confusing ESC/P language.
I am using Spyder 4 with anaconda (python 3.7) 64-bit, libusb 1.0.22b9, most recent pyusb on github (PyPi version got unimplemented error), and python-escpos 3.0a8. I believe they are all 64-bit as well. I have the libusb1.dll from 64x folder in my System32 and the 86x one in SysWoW64 as recommended. I also have Usbdk installed. Please let me know if you have any ideas to fix or if you need more details. Been googling this for like a week.
Isn't it a problem with the printer's USB mode setting?
TM-T20II printer has the modes of USB vender-defined class (COM Port) and USB printer class.
The VID/PID is USB vender-defined class=0x04b8:0x0202 and USB printer class=0x04b8:0x0e15 respectively.
TM-T20II Technical Reference Guide page 89
You should be able to change it with the printer's setting utility.
Alternatively, try changing the PID designation to 0x0202 as is, or try communicating as a COM port instead of a raw USB device.
Or it is possible that the advanced printer driver or Windows print spooler is already using the device and other programs cannot be used.
If the device driver is installed, try uninstalling it.
For anyone else with the same problem as me, what I did was I installed Epson's TM Virtual Port Driver and set the printer to a COM port. I then had to go into the printer's settings and manually change the port to the virtual one. I then altered my code to this:
from escpos import printer
pr = printer.Serial('COM2')
data= '''
hello world
'''
pr.text(data)
pr.close()
And it finally worked! You can see I cut my code down quite a bit. It turns out I don't even need to change the backend. So strange Usb doesn't work but serial does. As long as it works though! Thanks to kunif for the guidance to my solution, never even considered checking the serial ports.

How to change my ssl ciphers in ldaps request?

Dears~,
My environment is:
OS:Ubuntu 12.04.4 LTS
Python:Python 2.7.3
When use ldap connect to AD server over ssl. I got this error
"A TLS packet with unexpected length was received"
I have got the package by tcpdump and find
hello faild
Hello details
But when I use perl script in same environment is ok, and python script running in Ubuntu16 also connect successfully(only python in ubuntu12 not work)
When successfully connected the hello request will bring more ciphers than Ubuntu12.
Run well on Ubuntu16
When faild ,AD server could found
error log
My test script is:
import ldap
TIMEOUT = 30
DEBUG_LEVEL = 8191
TRACE_LEVEL = 10
AD_HOST = "10.29.137.100"
USERNAME = "username"
PASSWORD = "password"
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
ldap.set_option(ldap.OPT_DEBUG_LEVEL, 8191)
ldapConn = ldap.initialize("ldaps://" + AD_HOST + ":636",
trace_level=TRACE_LEVEL)
ldapConn.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
ldapConn.set_option(ldap.OPT_X_TLS_CIPHER_SUITE,'TLSv1:!NULL')
ldapConn.set_option(ldap.OPT_REFERRALS, 0)
ldapConn.set_option(ldap.OPT_NETWORK_TIMEOUT , TIMEOUT)
ldapConn.set_option(ldap.OPT_TIMEOUT , TIMEOUT)
ldapConn.simple_bind_s(USERNAME, PASSWORD)
My question is how to change ciphers in python scripts?
I found
ldapConn.set_option(ldap.OPT_X_TLS_CIPHER_SUITE,'TLSv1:!NULL')
not work for me. and now I have no idea where setting these cipher values. or what third party depend I can upgrade to support more ciphers.
Thanks~~~
You've just hit the python 2/3 wall.
Your script is python3 that you try to run in a python 2.7 environment which is not backward compatible. Only option is to install python3 on Ubuntu 12 and run it there with python3.X.
An example is shown here.
Like me today, you're probably in the situation explained here: https://github.com/python-ldap/python-ldap/issues/55 (and here https://github.com/pyldap/pyldap/issues/53):
Several, perhaps all set_option(OPT_X_TLS_*, ...) calls require a final set_option(ldap.OPT_X_TLS_NEWCTX, 0) call to submit all previous set_option() calls. Without OPT_X_TLS_NEWCTX, settings are effectively ignored.
=> You can either add ldap.set_option(ldap.OPT_X_TLS_CIPHER_SUITE,'TLSv1:!NULL') before the initialize call, or add ldapConn.set_option(ldap.OPT_X_TLS_NEWCTX, 0) before the bind.

How to automate shell interactive commands using Python pexpect module

I am trying to automate the setup of an application by performing SSH to the machine and goto /var/packages folder and execute the script.when the installation starts a set of interactive commands to be send based on the expected output.I found from google that pexpect can achieve this but i am unable to achieve the result that i wish. I am trying following code , can someone guide me how to achieve this as I am beginner to python.Any help would be appreciated. My application setup would look like this
[root#bits packages]# ./SHR_setup.bin -i console
Preparing to install...
Extracting the JRE from the installer archive...
Unpacking the JRE...
Extracting the installation resources from the installer archive...
Configuring the installer for this system's environment...
Launching installer...
===============================================================================
Choose Locale...
----------------
1- Deutsch
->2- English
3- Español
4- Français
5- Italiano
6- Nederlands
7- Português (Brasil)
CHOOSE LOCALE BY NUMBER: 2
I accept the terms of the License Agreement (Y/N): Y
Please hit Enter to continue:
Python Code
from pexpect import pxssh
import pexpect
try:
s = pxssh.pxssh()
hostname = '10.110.40.20'
username = 'admin'
password = 'admin123'
s.login(hostname, username, password)
s.sendline('cd /var/packages') # goto /var/packages folder
child = pexpect.spawn('./SHR_setup.bin -i console') # start the application setup in packages folder
child.expect('CHOOSE LOCALE BY NUMBER') # expect output like this
child.sendline('2')
s.prompt()
print s.before
except pxssh.ExceptionPxssh, e:
print 'pxssh failed on login'
print e
You should change
s.sendline('cd /var/packages')
child = pexpect.spawn('./SHR_setup.bin -i console')
to
s.sendline('cd /var/packages')
s.sendline('./SHR_setup.bin -i console')
spawn is supposed to run a program on the local host, not on the remote host.
You're on the right track with using the s.before log for debugging.
The app you're interacting with appears to be more screen-oriented than line-oriented, which can pose some difficulties, including ANSI escape sequences for color and position. Consider running child.expect('Something else'), some string which does reliably show up in before, then do a brief sleep(), then just "blindly" send "2" or "y" or whatever, pausing briefly between sends.

Python script keeps running when using pyRserve

I am trying to learn how to send a list of lists in Python to R -script which runs statistical methods and gives two or three data frames back to Python
I stumbled across the pyRserve package. I was able to follow the manual in their documentation and everything works great in command line (>>> ). When I run a script, it does not stop. I have installed Rserve package and started its service in RStudio. Below is the code:
import pyRserve
print "here1" #prints this line...
conn = pyRserve.connect(host='localhost', port=6311)
print "here2"
a= conn.eval('3+5')
print a
Can anyone please help?
The (docs) suggest:
$ python
>>> import pyRserve
>>> conn = pyRserve.connect()
And then go on with:
To connect to a different location host and port can be specified explicitly:
pyRserve.connect(host='localhost', port=6311)
This is not meant to indicate that both lines should be run. The second line should be viewed as a potential modifier for the first. So if you need an alternate address or port, then it should look like:
$ python
>>> import pyRserve
>>> conn = pyRserve.connect(host='localhost', port=6311)
Also note this caveat for windows users:
Note On some windows versions it might be necessary to always provide ‘localhost’ for connecting to a locally running Rserve instance.

Why can't I use Cocoa classes from my Python script?

Today is the first time I've used Python, so I'm sure this'll be an easy question.
I need to convert this Python script from a command line application: webkit2png. The end result will be a URL that returns an image of the webpage passed into it as a querystring param. I've achieved this on Windows with .NET and IE, Gecko and WebKit, but now need to do the same for Safari on OS X.
I think I've got it converted, but unfortunately I'm running into a problem running the script from Apache on OS X:
app = AppKit.NSApplication.sharedApplication()
# create an app delegate
delegate = AppDelegate.alloc().init()
AppKit.NSApp().setDelegate_(delegate)
# create a window
rect = Foundation.NSMakeRect(0,0,100,100)
win = AppKit.NSWindow.alloc()
win.initWithContentRect_styleMask_backing_defer_ (rect,
AppKit.NSBorderlessWindowMask, 2, 0)
The error is thrown on the final line "initWithContentRect...". The error I see is:
<class 'objc.error'>: NSInternalInconsistencyException - Error (1002) creating CGSWindow
args = ('NSInternalInconsistencyException - Error (1002) creating CGSWindow',)
message = 'NSInternalInconsistencyException - Error (1002) creating CGSWindow'
name = u'NSInternalInconsistencyException'
If I run the script on the command line (after removing the CGI stuff), it runs perfectly.
Here's the libraries I'm importing:
import cgi
import cgitb; cgitb.enable() # for troubleshooting
import sys
try:
import Foundation
import WebKit
import AppKit
import objc
except ImportError:
print "Cannot find pyobjc library files. Are you sure it is installed?"
sys.exit()
You cannot (usually) connect to the window server from a process not associated to a GUI user. See this Apple tech note.
Basically, it's a big no-no to use NSWindow etc. from the process spawned by Apache. The window server is not even guaranteed to exist if there's no GUI user logged in. So, you can't reliably do what you're trying to do.
The problem is that the WebKit which comes with OS X depends on the window server. One way out might be to install Qt, which hopefully has a backend of WebKit independent of the Core Graphics window server.

Categories

Resources