Using the pyperclip moudel shows gremlin when chinese in clipboard - python

I have an issue that exists only on the MacOs. It works fine on Windows.
I am using python3.
Here is the code:
# coding=utf-8
# using address you inputed to open a web map site
import webbrowser, sys, pyperclip
if len(sys.argv) > 1:
#get address from command line.
address = ' '.join(sys.argv[1:])
else:
#get address from clip board.
address = pyperclip.paste()
webbrowser.open('https://ditu.amap.com/search?query=' + address)
For example:
copy "New York" works.
But when you copy "和卉家园" which is a Chinese address, it will be a mojibake (garbled text),and this problem exists only in MacOs.
I have no idea how to solve it, please help me ,Thank you!

Related

Simple SFTP script

I am getting error failed to load hostkeys.
Hello,
I'm having trouble with a script i wrote to push a file to an SFTP. I'm using windows and the below is the code I have so far. The actual data manipulation and naming change works fine - it breaks at the SFTP portion. I'm hoping one of you guru's out there can help a newbie out.
import pandas as pd
import datetime
import pysftp
import sys
today = str(datetime.date.today().strftime("%m%d%y"))
report = pd.read_csv('C:\\Users\\nickkeith2\\PycharmProjects\\clt\\041719_clt_Facility_company_Inv.csv')
report.columns = report.columns.str.replace('_', ' ')
report.to_csv('C:\\Users\\nickkeith2\\PycharmProjects\\clt\\' + today + '_clt_Facility_company_Inv2.csv',
index=False)
remote_file = 'C:\\Users\\nickkeith2\\PycharmProjects\\clt\\' + today + '_clt_Facility_company_Inv2.csv'[1]
cnopts = pysftp.CnOpts()
cnopts.hostkeys.load("C:\\Users\\nickkeith2\\id_rsa.pub")
srv = pysftp.Connection(host="xx.xxx.xxx.xxx", username="sftpuser")
srv.put(remote_file)
srv.close()
print(report.columns)
I have tried various of combinations of using a key, not using a key and using a password instead - but no matter what it returns the error:
UserWarning: Failed to load HostKeys from C:\Users\nickkeith2\.ssh\known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
warnings.warn(wmsg, UserWarning)
I tried to create the folder in windows it specifies to put the key there but it will not allow me too. Thank you in advance on any insight you may be able to provide.
Transcribing Charles Duffy's comments as an answer:
An id_rsa.pub file is not a host key. To be clear, the host keys file is used by the client to make sure the host it's connecting to is genuine (it's a list of the known public keys identifying the remote servers, not anything to do with the public keys identifying the user who's logging in).
I was able to successfully complete this using paramiko instead of PYSFTP.
Thank you!
import pandas as pd
import datetime
import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
today = str(datetime.date.today().strftime("%m%d%y"))
report = pd.read_csv('C:\\Users\\nickkeith2\\PycharmProjects\\clt\\041719_marshall_Facility_clt_Inv.csv')
report.columns = report.columns.str.replace('_', ' ')
report.to_csv('C:\\Users\\nickkeith2\\PycharmProjects\\clt\\' + today + '_marshall_Facility_clt_Inv2.csv',
index=False)
remote_file = 'C:\\Users\\nickkeith2\\PycharmProjects\\clt\\' + today + '_marshall_Facility_clt_Inv2.csv'
ssh_client.connect(hostname="xxxxxxxx", username="xxxxxxxx", password="xxxxxxxxxx")
ftp_client=ssh_client.open_sftp()
ftp_client.put(remote_file, '\\Outbound\\'+ today + '_marshall_Facility_clt_Inv2.csv')
ftp_client.close()
print(report.columns)

Trying to pass extracted text from Tesseract OCR to custom google search

Having some trouble with a project and hopefully someone can help! I'm trying to take extracted text from tesseract OCR and use that text as the search query of Google Chrome searches. My shell script can extract the text and launch Chrome, but I cant figure out how to send the text to the searchbar of chrome. Below are some pictures of my script. I'm extremely new to coding, so any help is appreciated.
Shell script
echo "Realtime Screen OCR"
while true
do
echo "Waiting for trigger"
read
screencapture -R31,205,420,420 screens.png
tesseract screens.png ocr
OCR=`cat ocr.txt`
python3 launch1.py $OCR
##/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome $OCR
echo "Opened Chrome...waiting for next question"
done
Python Script
import urllib.parse
search_query = input("enter search query")
query_encoded = urllib.parse.quote_plus(search_query)
google_search_url = "http://www.google.com/search?q=" +
format(query_encoded)
import webbrowser
webbrowser.open(google_search_url)
It appears to me that the only step you're missing is grabbing the text in your Python code and passing it into the search query. You can do this using sys. In this example we replace the user-inputted text with retrieving the argument from the command line, assuming that this is what you're trying to do.
import urllib.parse
import sys
search_query = sys.argv[1]
query_encoded = urllib.parse.quote_plus(search_query)
google_search_url = "http://www.google.com/search?q=" +
format(query_encoded)
import webbrowser
webbrowser.open(google_search_url)

NET-SNMP + Python Mac Address shows as \x00\

Hey Im trying to get the MAC-address via ipNetToMediaPhysAddress which works fine when using the netsnmp.snmpget command but when saving that into a variable(tuple?) and printing it out via "print" the mac-address looks like this.
('\x00\n\xb7\x9c\x93\x80',)
Code looks like this,
mac = netsnmp.Varbind("ipNetToMediaPhysAddress."+i+"."+ipadd)
macadd = netsnmp.snmpget(mac, Version = 2, DestHost = ip, Community = comm)
print '%-15s' % macadd
So what do I need to do? I just want it to look like a normal MAC address.
Maybe a call to hexlify is enough
from binascii import hexlify
mac = netsnmp.Varbind("ipNetToMediaPhysAddress."+i+"."+ipadd)
macadd = netsnmp.snmpget(mac, Version = 2, DestHost = ip, Community = comm)
print hexlify(macadd[0])

Getting my IP using python

I am trying to update my rackspace dns with my IP using a python script.
My script works when I manually enter an IP in it, but dosen't when I get it from the outside, why?
This WORKS:
#!/usr/bin/env python
import clouddns
import requests
r= requests.get(r'http://curlmyip.com/')
ip= '4.4.4.4'
dns = clouddns.connection.Connection('******','********************')
domain = dns.get_domain(name='reazem.net')
record = domain.get_record(name='ssh.reazem.net')
record.update(data=ip, ttl=600)
This DOESN'T:
#!/usr/bin/env python
import clouddns
import requests
r= requests.get(r'http://curlmyip.com/')
**ip= '{}'.format(r.text)**
dns = clouddns.connection.Connection('******','********************')
domain = dns.get_domain(name='reazem.net')
record = domain.get_record(name='ssh.reazem.net')
record.update(data=ip, ttl=600)
Note: print '{}'.format(r.text) succesfully outputs my ip.
Helping you helping me: I just noticed that print '{}'.format(r.text) adds an extra line, how do I avoid that?
For those interested: https://github.com/rackspace/python-clouddns
Try ip = r.text.strip() to remove the extra newline.

Python requests fails to get webpages

I am using Python3 and the package requests to fetch HTML data.
I have tried running the line
r = requests.get('https://github.com/timeline.json')
, which is the example on their tutorial, to no avail. However, when I run
request = requests.get('http://www.math.ksu.edu/events/grad_conf_2013/')
it works fine. I am getting errors such as
AttributeError: 'MockRequest' object has no attribute 'unverifiable'
Error in sys.excepthook:
I am thinking the errors have something to do with the type of webpage I am attempting to get, since the html page that is working is just basic html that I wrote.
I am very new to requests and Python in general. I am also new to stackoverflow.
As a little example, here is a little tool which I developed in order to fetch data from a website, in this case IP and show it:
# Import the requests module
# TODO: Make sure to install it first
import requests
# Get the raw information from the website
r = requests.get('http://whatismyipaddress.com')
raw_page_source_list = r.text
text = ''
# Join the whole list into a single string in order
# to simplify things
text = text.join(raw_page_source_list)
# Get the exact starting position of the IP address string
ip_text_pos = text.find('IP Information') + 62
# Now extract the IP address and store it
ip_address = text[ip_text_pos : ip_text_pos + 12]
# print 'Your IP address is: %s' % ip_address
# or, for Python 3 ... #
# print('Your IP address is: %s' % ip_address)

Categories