How to Read the serial port name from text box (editable) - python

I am trying to open the serial port in Linux with Python 2.7 PyQt4 with the below code and it works fine:
serialport.port = "/dev/ttyACM1"
serialport.baudrate = 115200
serialport.open()
I don't want to hard-code the serial port name as above. I want to take the serial port name as the input from user from an editable text box:
textbox.setText("/dev/ttyACM1")
serialport.port = textbox.text()
serialport.baudrate = 115200
serialport.open()
But I am unable to convert textbox.text() format to serialport.port.
The following error occurs:
ValueError: "port" must be None or a string, not < class 'PyQt4.QtCore.QString' >

You are using PyQt and have something like this:
w = QWidget()
textbox = QLineEdit(w)
right?!
The error message tells you that the result of textbox.text()is of type QString. But you need a string instead.
You can simply convert the result using str(textbox.text())
serialport.port = str(textbox.text())
should solve the problem.

Related

How to authenticate proxy with username and password in Selenium using Python

I am trying to authenticate proxy with username and password in Selenium using Python but the current code is not working. I have tried many solutions but none of them worked.
Proxy example,
IP = xxx.xx.xx.xx
PORT = xxxxx
USERNAME = USERNAME
PASSWORD = PASSWORD
I have used the following code,
driver.execute_script("""
Services.prefs.setIntPref('network.proxy.type', 1);
Services.prefs.setCharPref("network.proxy.http", arguments[0]);
Services.prefs.setIntPref("network.proxy.http_port", arguments[1]);
Services.prefs.setCharPref("network.proxy.ssl", arguments[0]);
Services.prefs.setIntPref("network.proxy.ssl_port", arguments[1]);
Services.prefs.setCharPref('network.proxy.socks', arguments[4]);
Services.prefs.setIntPref('network.proxy.socks_port', arguments[5]);
Services.prefs.setCharPref('network.proxy.socks_username', arguments[6]);
Services.prefs.setCharPref('network.proxy.socks_password', arguments[7]);
""", http_addr, http_port, ssl_addr, ssl_port, socks_addr, socks_port, socks_username, socks_password)
I have tried some other code snippets also. I tried to place values into alert boxes also.
You can achieve this by using AutoIt. And it has Python binding PyAutoIt. Once you installed PyAutoIt using PIP - pip install PyAutoIt, the following code does your job.
import autoit
autoit.win_wait_active("Authentication Required") # title of the dialog box to wait. so it will wait for the Authentication Required dialog
autoit.send("username", 1) # second parameter is the mode (changes how "keys" is processed)
autoit.send("{TAB}") # press tab key to go to the password field
autoit.send("password", 1)
autoit.send("{Enter}") # press enter key
For more information about the second parameter in the send method, here is the code,
def send(send_text, mode=0):
"""
Sends simulated keystrokes to the active window.
:param send_text:
:param mode: Changes how "keys" is processed:
flag = 0 (default), Text contains special characters like + and ! to
indicate SHIFT and ALT key presses.
flag = 1, keys are sent raw.
:return:
"""
AUTO_IT.AU3_Send(LPCWSTR(send_text), INT(mode))

Python getting ips and macs from network

I'm trying to get all IP's and their associated MAC address from the network.
Till now, i have the following code:
eth = Ether(dst = "ff:ff:ff:ff:ff:ff")
arp = ARP(pdst = '198.13.13.1')
answered = srp1(eth / arp)
print answered[1].hwsrc
print answered[1].psrc
But this only gets me the MAC of what Ip i input.
I think i have to use:
answered, unanswered = srp(eth/arp)
And modify pdst with the address of the subnet but i don't know how to do it.
Any ideas?
You just need to enter your network as pdst field. You want to use a timeout because some probes probably won't get an answer. For example:
ans, unans = srp(Ether(dst=ETHER_BROADCAST) / ARP(pdst="198.13.13.0/24"), timeout=1)
Then you need to parse ans. If you want to get the MAC & IP addresses associated, you can create a list of tuples. For example:
res = [(pr.psrc, pr.hwsrc) for _, pr in ans]
print res
You can also use arping(), a specialized function for that purpose:
ans, unans = arping("198.13.13.0/24")
ans.show()

How to control windows master audio in python script

going off of the suggestion by abarnert in Python: Change Windows 7 master volume
I'm trying to write a python script to control the master volume in windows 7
I understand that in C++ this can be done like so:
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
mmde = CoCreateInstance(
CLSID_MMDeviceEnumerator, NULL,
CLSCTX_ALL, IID_IMMDeviceEnumerator,
(void**)&pEnumerator);
mmd = mmde.GetDefaultAudioEndpoint(eRender, eMultimedia);
mgr = mmd.Activate(IID_IAudioSessionManager);
sav = mgr.GetSimpleAudioVolume(None, True);
sav.SetMasterVolume(0.5);
I'm trying to get that functionality in python using pywin32, but I find myself stuck. The code I have so far is:
import pythoncom
CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator)
IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator)
mmde = pythoncom.CoCreateInstance(CLSID_MMDeviceEnumerator, None, CLSCTX_ALL, IID_IMMDeviceEnumerator)
mmd = mmde.GetDefaultAudioEndpoint(eRender, eMultimedia)
mgr = mmd.Activate(IID_IAudioSessionManager)
sav = mgr.GetSimpleAudioVolume(None, True)
sav.SetMasterVolume(0.5)
CoCreateInstance wants the class ID (CLSID) of the MMDeviceEnumerator, but doesn't seem to have any function like __uuidof() to use to get the class ID. (Not that I could find anyway.)
Does anyone have any ideas / suggestions? I'm new to both COM/OLE programming and pywin32 and feeling a little lost.
From the documentation
PyIUnknown = CoCreateInstance(clsid, unkOuter , context , iid )
where
clsid : PyIID
Class identifier (CLSID) of the object
A PyIID object is used whenever a COM GUID is used. PyIID objects can be created using the pywintypes.IID() function, although all functions that accept a GUID also accept a string in the standard GUID format.
PyIID = IID(iidString, is_bytes )
where iidString is a string representation of an IID, or a ProgID.
MMDeviceEnumerator CLSID is BCDE0395-E52F-467C-8E3D-C4579291692E
so try this
PyIID = IID("BCDE0395-E52F-467C-8E3D-C4579291692E", is_bytes )

Convert MAC to aa:bb:cc:dd:11:22

I'm creating a SNMP program to list interfaces (with ip, mask and mac) of devices. I'm using NetSnmp to get the macaddress but the output looks like this ('\x00PV\xaf\x00v',)
This is the SNMP request:
oidmac = netsnmp.Varbind("ifPhysAddress."+i)
mac = netsnmp.snmpget(
oidmac,
Version = 2,
DestHost = sys.argv[2],
Community = sys.argv[1])
Info about the code ...
sys.argv[1] = Community string
sys.argv[2] = IP of the SNMP agent
i = a variable with the interface ID.
How can I convert the string to an MAC address in format aa:bb:cc:dd:11:22 ?
In Python2, it's very simple
>>> ":".join(x.encode('hex') for x in '\x00PV\xaf\x00v')
'00:50:56:af:00:76'
For Python3, you can try something like this
>>> "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*b'\x00PV\xaf\x00v')
'00:50:56:af:00:76'
Use :02X (capital X) if you want uppercase hex codes

Reading bytes from ser.read()

I have one XBee router (XB-X) connected to an ultrasonic range sensor and the other XBee coordinator (XB-Y) connected to my PC. XB-X will send the sensor reading to XB-Y. I am trying to extract the bytes from ser.read() on my PC using Python.
I got a weird bytes of string consist of ">>~}3#zi167.89" on output, may I know how to extract only the floating point number (167.89 in this example)? The number of bytes are specified by setting ser.read(size=xx). Is there any alternative way of doing it?
import serial
import zigbee
from xbee import ZigBee
import re
ser = serial.Serial("COM4", 9600)
while True:
try:
input = ser.read(20)
m = re.search(r'(\d+\.\d+)', input)
if m:
num = m.group()
# statements...
It is working now, carelessly typed 'group' as 'groups'.
Just use a regular expression:
import re
input = ser.read()
m = re.search(r'(?P<num>\d+\.\d+)', input)
if m:
num = float(m.group('num'))

Categories

Resources