Odd behavior with urllib.request.urlopen in Ubuntu 18.04 - python-3.x

I have been working through an online class on python with Coursera (this is not homework) and have been having a problem with urllib.request.urlopen for some urls. For the url hardcoded into the code below, the command urllib.request.urlopen(serviceurl, context=ctx).read().decode() times out. If another url is used... say http://www.woot.com is used data is returned.
I have tried this on two separate Ubuntu machines at my location, both running 18.04 (with 3.6.7 which is default) and 3.7.3 via Anaconda.
I have even reinstalled Ubuntu with the same results.
Strangely, if I include a timeout parameter (for example, urllib.request.urlopen(serviceurl, timeout=1, context=ctx).read().decode()), data is returned.
Also, this program runs successfully (regardless of url) with no timeout parameter on a macbook air running 3.6.4
import urllib.request
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
address = input('Enter Locaton: ')
if len(address) < 1:
serviceurl = 'http://py4e-data.dr-chuck.net/comments_42.xml?'
else:
serviceurl = address
s = urllib.request.urlopen(serviceurl, context=ctx).read().decode()
print(s)
I seem to be the only one having this issue and it has me stumped. I am just beginning to get familiar with python (C, C#, Java are more familiar). Any ideas would be appreciated.

Answered (I think) my own question. Looks like the website does not like IP6 sockets. Was able to trace the hang back to socket.py. First address used in create connection is an IP6 address and port which does not return anything. Adding a timeout caused the code to select the next address and port from the list which was IP4 and that worked. For the time being I disabled IP6 in Ubuntu 18.04 to force IP4 use.

Related

s.bind((hostMACAddress,port)). OSError: [WinError 10049] The requested address is not valid in its context

I am trying to use the Bluetooth library in python 3 to enable me to allow a robot I am making to communicate with my PC. I have created a Bluetooth server that uses MAC addresses. unfortunately I have hit a halt in my program on this line:
s.bind((hostMACAddress,port))
OSError: [WinError 10049] The requested address is not valid in its context
For some reason it does not seem to like the MAC address (I have checked over the MAC address several times now and it is definitely correct). Personally, I would rather not want to use python 2 as the robot I am using is only compatible with python 3 and by using python 2 the whole idea would not work.
"""
A simple Python script to receive messages from a client over
Bluetooth using Python sockets (with Python 3.3 or above).
"""
import socket
hostMACAddress = 'xx:xx:xx:xx:xx:xx' # The MAC address of a Bluetooth adapter on the server. The server might have multiple Bluetooth adapters.
port = 3 # 3 is an arbitrary choice. However, it must match the port used by the client.
backlog = 1
size = 1024
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)
try:
client, address = s.accept()
while 1:
data = client.recv(size)
if data:
print(data)
client.send(data)
except:
print("Closing socket")
client.close()
s.close()
Here is the guide I have been following:
https://blog.kevindoran.co/bluetooth-programming-with-python-3/
These lines I have used alternatively to using MAC addresses (I am have tried using both):
hostIPAddress = "x.x.x.x"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((hostIPAddress,22))

Python requests_html render runs forever on certain URLs

I am trying to write a simple script that given an arbitrary URL will return the title tag of that website. Because many of the URLs I want to resolve need to have JavaScript enabled, I need to use something like requests_html's render function to do this. However, I have encountered an issue with the library where the example URL below never terminates. I have tried the timeout arg of the render call and it did not work. Can anyone help me figure out how to get this to timeout properly or some other work around to make sure it doesn't get stuck?
This is my current code that does not terminate (it gets stuck on the render call):
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('http://shan-shui-inf.lingdong.works/')
# render with JS
r.html.render(sleep = 1, keep_page=True)
# Also does not work: r.html.render(sleep = 1, keep_page=True, timeout = 3)
title = r.html.find('title', first=True).full_text
I have already tried solutions like: Timeout on a function call and Python timeout decorator which still did not timeout strangely enough.
NOTE: I am using Python 3.7.4 64-bit on Windows 10.
I would suggest to put r.session.close() at last. This worked for me.
Ok I'm quite late here,
This is what I've done:
pip install -U pyppeteer
(pip installed the 0.2.6 version for me)
Then it worked somehow
(unrelated)
If you want the Chromium browser to appear on the screen you'll need to change requests_html.py (somewhere in site-packages)'s 714th line,
headless=True -> headless=False

Pyserial: termios.error: (22, 'Invalid argument') while reading from virtual serial port

Is there any posibility to read from virtual serial ports without mentioned error?
I'm trying to reach data sent from symbol/zebra barcode scanner li4278. Device is working in simple com port emulation mode and it's under /dev/usb/hiddev0 as serial port.
I'm sure that emulation works fine because CuteCom can read from it, and also this simple pyton script works too:
defaultDevice = '/dev/usb/hiddev0'
inDev = open(defaultDevice, 'rb')
while True:
inBytes = inDev.read(1)
for x in inBytes:
print(x)
But anytime I'm trying to read it using pyserial with such a minimal code like this:
with serial.Serial('/dev/usb/hiddev0', timeout=1) as ser:
x = ser.read()
print(x)
the same error occurs: termios.error: (22, 'Invalid argument'). It's like it can't read from virtual serial ports.
And no, setting args to rtscts=True, dsrdtr=True doesn't work.
I have the same '22, invalid argument' error.
Linux Mint 18, 64 bit, Python 3.7, pyserial 3.4.
Anyone knows what's the thing?

Get data into Graphite/Carbon using Python3

I've got a Grafana docker image running with Graphite/Carbon. Getting data using CLI works, example:
echo "local.random.diceroll $(((RANDOM%6)+1)) `date +%s`" | nc localhost 2003;
The following Python 2 code also works:
sock = socket.socket()
sock.connect((CARBON_SERVER, CARBON_PORT))
sock.sendall(message)
sock.close()
message is a string containing key value timestamp and this works, the data can be found. So the Grafana docker image is accepting data.
I wanted to get this working in Python 3, but the sendall function requires bytes as parameter. The code change is:
sock = socket.socket()
sock.connect((CARBON_SERVER, CARBON_PORT))
sock.sendall(str.encode(message))
sock.close()
Now the data isn't inserted and I can't figure out why. I tried this on a remote machine (same network) and on the local server. I also tried several packages (graphiti, graphiteudp), but they all seem to fail to insert the data. They also don't show any error message.
The simple example for graphiteudp doesn't work either on the Github page
Got an idea what I'm doing wrong?
You can add \n to the message you send. I have tried it with Python 3, and that works.

Can not pass OSC data using IMU manufacturer's python2.7 example script

I am working with a high refresh rate IMU (x-IO technologies NGIMU) which outputs all data in osc format. The manufacturer provides the following python script to serve the data on linux platforms ( I am running Ubuntu 16.04)
'''
NGIMU Demo python v2.7 script written by Tom Mitchell (teamxe.co.uk) 2016
Requires pyOSC https://trac.v2.nl/wiki/pyOSC
'''
import socket, OSC, threading, time
# Change this to the NGIMU IP address
send_address = '192.168.1.1', 9000
# Set the NGIMU to send to this machine's IP address
c = OSC.OSCClient()
c.connect(send_address)
msg = OSC.OSCMessage()
msg.setAddress('/wifi/send/ip')
msg.append(str(socket.gethostbyname(socket.gethostname())))
c.send(msg)
c.close()
# Set up receiver
receive_address = '192.168.1.2', 8000
s = OSC.OSCServer(receive_address)
s.addDefaultHandlers()
def sensorsHandler(add, tags, args, source):
print add + str(args)
def quaternionHandler(add, tags, args, source):
print add + str(args)
def batteryHandler(add, tags, args, source):
print add + str(args)
# Add OSC handlers
s.addMsgHandler("/sensors", sensorsHandler)
s.addMsgHandler("/quaternion", quaternionHandler)
s.addMsgHandler("/battery", batteryHandler)
# Start OSCServer
print "\nUse ctrl-C to quit."
st = threading.Thread(target = s.serve_forever)
st.start()
# Loop while threads are running
try :
while 1 :
time.sleep(10)
except KeyboardInterrupt :
print "\nClosing OSCServer."
s.close()
print "Waiting for Server-thread to finish"
st.join()
print "Done"
The IMU hosts its own network which I connect to with the computer that is to receieve the data.
I have installed pyOSC from the location referenced in the script.
When I run the script, no data is delivered, only the message "Use ctrl-C to quit".
All connections seem to take place properly. When the script is running, I can see the udp connection at the correct ip and port using the Ubuntu firewall configuration gui. I have tried disabling the firewall but that had no effect.
Separately, I have used another computer to send udp packets to that ip and port and confirmed their receipt.
To say that I am a coding novice is far too generous. Nonetheless, I need to get this script running. Any help you can offer is greatly appreciated.
The problem is that
socket.gethostbyname(socket.gethostname())
is not setting the correct IP. You should change to
msg.setAddress('/wifi/send/ip')
msg.append('192.168.1.2')

Resources