Python and Cors
Deve Krehbiel <[email protected]> Sun, 27 Aug 2017 11:10:15 -0500
| Newsgroups | gmane.comp.python.twisted.web |
|---|---|
| Message-ID | <CADfFrhQjt4F6LVWYU-c7NVQ=H-UK+FcH7UCm9vQREN8Y0eTcsA@mail.gmail.com> |
--===============6455135163953911921==
Content-Type: multipart/alternative; boundary="001a114d6b50e670920557be69f6"
--001a114d6b50e670920557be69f6
Content-Type: text/plain; charset="UTF-8"
First post so forgive me if I am not explaining this right.
I am trying to make a baby cam and then heavily document it for everyone to
have access to. I am using a Raspberry Pi with Rasbian. I am using
mjpg-streamer for the streaming with no problems.
The problem is I am also using two servos for pan and tilt. To accomplish
this I am using an Arduino Uno. This is so I can add IR lighting for night
vision, etc later. I am not a programmer so this is getting frustrating
after a few weeks of failure.
How is it failing you might ask..well, its working perfectly with only ONE
exception:
Every time I push the button for left/right/up/down I get this:
XMLHttpRequest cannot load http://192.168.1.122:81/servos.rpy?value=100P.
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://192.168.1.122:9000' is therefore not allowed
access.
On the local area network, it still works, but taking it to the internet it
does not.
Here are the two programs that make this happen. One is servos.rpy and the
other is the supporting HTML/Get script;
****
servos.rpy
-----------------------------------
# Import necessary files
import serial
from twisted.web.resource import Resource
# Setup Arduino at correct speed
try:
arduino = serial.Serial('/dev/ttyACM0', 9600)
except:
arduino = serial.Serial('/dev/ttyACM1', 9600)
class MoveServo(Resource):
isLeaf = True
def render_GET(self,request):
try:
# Send value over serial to the Arduino
arduino.write(request.args['value'][0])
return 'Success'
except:
return 'Failure'
resource = MoveServo()
***
Now for the HTML/Get script:
**
<!doctype html>
<html>
<head>
<title>Make Use Of DIY Security Camera</title>
<style type="text/css">
#container {
/* center the content */
margin: 0 auto;
text-align: center;
}
</style>
</head>
<body>
<div id="container">
<img src="/?action=stream" /><br>
<script src="
https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script><br
/>
<button onclick="servos.move('P', 10)">Left</button>
<button onclick="servos.move('P', -10)">Right</button>
<button onclick="servos.move('T', -10)">Down</button>
<button onclick="servos.move('T', 10)">Up</button>
</div>
</body>
<script>
var servos;
$( document ).ready(function() {
servos = moveServos();
});
function moveServos() {
// Store some settings, adjust to suit
var panPos = 70,
tiltPos = 90,
tiltMax = 170,
tiltMin = 45,
panMax = 170,
panMin = 20;
return {
move:function(servo, adjustment) {
var value;
if(servo == 'P') {
if(!((panPos >= panMax && adjustment > 0) || (panPos <= panMin &&
adjustment < 0))) {
// Still within allowed range, "schedule" the movement
panPos += adjustment;
}
value = panPos + 'P';
}
else if(servo == 'T') {
if(!((tiltPos >= tiltMax && adjustment > 0) || (tiltPos <= tiltMin &&
adjustment < 0))) {
// Still within allowed range, "schedule" the movement
tiltPos += adjustment;
}
value = tiltPos + 'T';
}
// Use AJAX to actually move the servo
$.get('http://192.168.1.122:81/servos.rpy?value=' + value);
},
}
}
</script>
</html>
*****
Twisted is started: sudo twistd -n web -p 81 --path /usr/local/www/
I would be forever grateful if someone could take a serious look at this
and tell me where I am going wrong. Once this is fixed, I can start
documenting the entire system for everyone. Thank you!
Deve
--001a114d6b50e670920557be69f6
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">First post so forgive me if I am not explaining this right=
.=C2=A0<div><br></div><div>I am trying to make a baby cam and then heavily =
document it for everyone to have access to. I am using a Raspberry Pi with =
Rasbian. I am using mjpg-streamer for the streaming with no problems.=C2=A0=
</div><div><br></div><div>The problem is I am also using two servos for pan=
and tilt. To accomplish this I am using an Arduino Uno. This is so I can a=
dd IR lighting for night vision, etc later. I am not a programmer so this i=
s getting frustrating after a few weeks of failure.</div><div><br></div><di=
v>How is it failing you might ask..well, its working perfectly with only ON=
E exception:</div><div>Every time I push the button for left/right/up/down =
I get this:</div><div><br></div><div>XMLHttpRequest cannot load <a href=3D"=
http://192.168.1.122:81/servos.rpy?value=3D100P">http://192.168.1.122:81/se=
rvos.rpy?value=3D100P</a>. No 'Access-Control-Allow-Origin' header =
is present on the requested resource. Origin '<a href=3D"http://192.168=
.1.122:9000">http://192.168.1.122:9000</a>' is therefore not allowed ac=
cess.</div><div><br></div><div>On the local area network, it still works, b=
ut taking it to the internet it does not.</div><div><br></div><div>Here are=
the two programs that make this happen. One is servos.rpy and the other is=
the supporting HTML/Get script;</div><div>****</div><div>servos.rpy</div><=
div>-----------------------------------</div><div><div># Import necessary f=
iles</div><div>import serial</div><div>from twisted.web.resource import Res=
ource</div><div># Setup Arduino at correct speed</div><div>try:</div><div>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 arduino =3D serial.Serial('/dev/ttyACM0'=
;, 9600)</div><div>except:</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 arduino =
=3D serial.Serial('/dev/ttyACM1', 9600)</div><div><br></div><div>cl=
ass MoveServo(Resource):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 isLeaf =3D T=
rue</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 def render_GET(self,request):</di=
v><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 try:</div><d=
iv>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <span style=3D"w=
hite-space:pre"> </span># Send value over serial to the Arduino</div><div>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 arduino.write(request.args['value'][0])</div><div>=C2=A0=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 return 'Success'</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 except:</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return 'Failure'</=
div><div><br></div><div>resource =3D MoveServo()</div></div><div>***</div><=
div><br></div><div>Now for the HTML/Get script:</div><div><br></div><div>**=
</div><div><div><!doctype html></div><div><html></div><div><spa=
n style=3D"white-space:pre"> </span><head></div><div><span style=3D"w=
hite-space:pre"> </span><title>Make Use Of DIY Security Camera</t=
itle></div><div><span style=3D"white-space:pre"> </span><style type=
=3D"text/css"></div><div><span style=3D"white-space:pre"> </=
span>#container {</div><div><span style=3D"white-space:pre"> </span>/* c=
enter the content */</div><div><span style=3D"white-space:pre"> </span>m=
argin: 0 auto;<span style=3D"white-space:pre"> </span></div><div><span styl=
e=3D"white-space:pre"> </span>text-align: center;</div><div><span style=
=3D"white-space:pre"> </span>}</div><div><span style=3D"white-space:pre">=
</span></style></div><div><span style=3D"white-space:pre"> </span>&=
lt;/head></div><div><span style=3D"white-space:pre"> </span><body>=
</div><div><span style=3D"white-space:pre"> </span><div id=3D"cont=
ainer"></div><div><span style=3D"white-space:pre"> </span><img=
src=3D"/?action=3Dstream" /><br></div><div><span style=
=3D"white-space:pre"> </span><script src=3D"<a href=3D"https://aj=
ax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">https://ajax.google=
apis.com/ajax/libs/jquery/3.1.0/jquery.min.js</a>"></script>&=
lt;br /></div><div><span style=3D"white-space:pre"> </span><button =
onclick=3D"servos.move('P', 10)">Left</button></=
div><div><span style=3D"white-space:pre"> </span><button onclick=3D&qu=
ot;servos.move('P', -10)">Right</button></div><div><s=
pan style=3D"white-space:pre"> </span><button onclick=3D"servos.m=
ove('T', -10)">Down</button></div><div><span style=3D=
"white-space:pre"> </span><button onclick=3D"servos.move('T&#=
39;, 10)">Up</button></div><div><span style=3D"white-space:pr=
e"> </span></div></div><div><span style=3D"white-space:pre"> </span>=
</body></div><div><span style=3D"white-space:pre"> </span><script&=
gt;</div><div><span style=3D"white-space:pre"> </span>var servos;</div><di=
v><span style=3D"white-space:pre"> </span>$( document ).ready(function() {=
</div><div><span style=3D"white-space:pre"> </span>servos =3D moveServos(=
);</div><div><span style=3D"white-space:pre"> </span>});</div><div><span s=
tyle=3D"white-space:pre"> </span>function moveServos() {</div><div><span s=
tyle=3D"white-space:pre"> </span>// Store some settings, adjust to suit</=
div><div><span style=3D"white-space:pre"> </span>var panPos =3D 70,=C2=A0=
</div><div><span style=3D"white-space:pre"> </span>tiltPos =3D 90,=C2=A0=
</div><div><span style=3D"white-space:pre"> </span>tiltMax =3D 170,=C2=
=A0</div><div><span style=3D"white-space:pre"> </span>tiltMin =3D 45,=C2=
=A0</div><div><span style=3D"white-space:pre"> </span>panMax =3D 170,=C2=
=A0</div><div><span style=3D"white-space:pre"> </span>panMin =3D 20;</di=
v><div><span style=3D"white-space:pre"> </span>return {</div><div><span s=
tyle=3D"white-space:pre"> </span>move:function(servo, adjustment) {</div=
><div><span style=3D"white-space:pre"> </span>var value;</div><div><spa=
n style=3D"white-space:pre"> </span>if(servo =3D=3D 'P') {</div=
><div><span style=3D"white-space:pre"> </span>if(!((panPos >=3D pan=
Max && adjustment > 0) || (panPos <=3D panMin && adju=
stment < 0))) {</div><div><span style=3D"white-space:pre"> </span>=
// Still within allowed range, "schedule" the movement</div><div>=
<span style=3D"white-space:pre"> </span>panPos +=3D adjustment;</div>=
<div><span style=3D"white-space:pre"> </span>}</div><div><span style=
=3D"white-space:pre"> </span>value =3D panPos + 'P';</div><div=
><span style=3D"white-space:pre"> </span>}</div><div><span style=3D"whi=
te-space:pre"> </span>else if(servo =3D=3D 'T') {</div><div><sp=
an style=3D"white-space:pre"> </span>if(!((tiltPos >=3D tiltMax &am=
p;& adjustment > 0) || (tiltPos <=3D tiltMin && adjustmen=
t < 0))) {</div><div><span style=3D"white-space:pre"> </span>// St=
ill within allowed range, "schedule" the movement</div><div><span=
style=3D"white-space:pre"> </span>tiltPos +=3D adjustment;</div><div=
><span style=3D"white-space:pre"> </span>}</div><div><span style=3D"wh=
ite-space:pre"> </span>value =3D tiltPos + 'T';</div><div><spa=
n style=3D"white-space:pre"> </span>}</div><div><span style=3D"white-sp=
ace:pre"> </span>// Use AJAX to actually move the servo</div><div><span=
style=3D"white-space:pre"> </span>$.get('<a href=3D"http://192.168=
.1.122:81/servos.rpy?value=3D">http://192.168.1.122:81/servos.rpy?value=3D<=
/a>' + value);</div><div><span style=3D"white-space:pre"> </span>},<=
/div><div><span style=3D"white-space:pre"> </span>}</div><div><span style=
=3D"white-space:pre"> </span>}</div><div><span style=3D"white-space:pre"> =
</span></script></div><div></html></div></div><div><br></div><d=
iv>*****</div><div>Twisted is started: =C2=A0sudo twistd -n web -p 81 --pat=
h /usr/local/www/</div><div><br></div><div>I would be forever grateful if s=
omeone could take a serious look at this and tell me where I am going wrong=
. Once this is fixed, I can start documenting the entire system for everyon=
e. Thank you!</div><div><br clear=3D"all"><div><div class=3D"gmail_signatur=
e"><div dir=3D"ltr"><div><div dir=3D"ltr"><div>Deve</div></div></div></div>=
</div></div>
</div></div>
--001a114d6b50e670920557be69f6--
--===============6455135163953911921==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline
X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KVHdpc3RlZC13
ZWIgbWFpbGluZyBsaXN0ClR3aXN0ZWQtd2ViQHR3aXN0ZWRtYXRyaXguY29tCmh0dHBzOi8vdHdp
c3RlZG1hdHJpeC5jb20vY2dpLWJpbi9tYWlsbWFuL2xpc3RpbmZvL3R3aXN0ZWQtd2ViCg==
--===============6455135163953911921==--