CORS error on comunication between Angular 6 client and Tornado server
I am trying to establish a comunication via socket.io socket between an angular 6 client and a tornado server. i get the followint errors:
on the client side:
Access to XMLHttpRequest at 'http://127.0.0.1:8888/socket.io/?
EIO=3&transport=polling&t=MSHp-m5' from origin 'http://localhost:4200' has
been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is
present on the requested resource.
on the server side:
WARNING:tornado.access:404 GET /socket.io/?EIO=3&transport=polling&t=MSHp-m5
(127.0.0.1) 1.00ms
I have a server Written in Tornado written in python 2.7, here is the code:
from tornado import websocket, web, ioloop , httpserver
import json
import socketio
from urlparse import urlparse
cl =
class SocketHandler(websocket.WebSocketHandler):
def check_origin(self, origin):
print "check origin"
return True
def open(self):
if self not in cl:
cl.append(self)
self.write_message("connection opened")
def on_close(self):
if self in cl:
cl.remove(self)
print "connection closed"
def on_message(self, message):
print "Message received: ".format(message)
self.write_message("message received")
if __name__ == '__main__':
app = web.Application([
(r'/ws', SocketHandler),
# (r'/api', ApiHandler),
(r'/(favicon.ico)', web.StaticFileHandler, 'path': '../'),
(r'/(rest_api_example.png)', web.StaticFileHandler, 'path': './'),
], user_settings="websocket_allow_origin": "*")
app.listen(8888)
ioloop.IOLoop.instance().start()
I also have a client written in Angular 6 who uses socket.io to start a session with the server. the client at the moment run on localhost:4200 but in the future will run on a different machine then the server and will not be rendered by the server, means that i can't just run the client on the same port as the server because it doesn't fit my needs.
here is the angular service code from the which deals with the comunication to the server.:
import Injectable from '@angular/core';
import HttpClient from '@angular/common/http';
import Observable,BehaviorSubject from 'rxjs';
import delay,map,catchError from 'rxjs/operators';
import * as io from 'socket.io-client';
@Injectable(providedIn: 'root')
export class PysystemRunnerService
private PDB_WEB_PORT = "23456";
private editUrl: string = 'http://127.0.0.1:' + this.PDB_WEB_PORT +
'/l1commands/pysystem_runner_edit_list';
private socket;
private socket_port = "8888";
private socketUrl = "ws://127.0.0.1:" + this.socket_port + "/ws"
constructor(private http: HttpClient)
this.socket = io(this.socketUrl);
handleEdit(data)
var ans = this.http.post(this.editUrl, data).pipe(
delay(100),
map((data: any) => (data.data
handleRun(data)
///////////////socket logic////////////////
////////////////////////////
public getMessages = () =>
return Observable.create((observer) =>
this.socket.on('new-message', (message) =>
observer.next(message);
);
);
//handle http errors
private handleError(error: any)
let errMsg = (error.message) ? error.message :
error.status ? `$error.status - $error.statusText` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
I was reading the Tornado documantation and it says that making the check_origin function return True should solve the CORS issue but it didn't.
python angular socket.io cors tornado
add a comment |
I am trying to establish a comunication via socket.io socket between an angular 6 client and a tornado server. i get the followint errors:
on the client side:
Access to XMLHttpRequest at 'http://127.0.0.1:8888/socket.io/?
EIO=3&transport=polling&t=MSHp-m5' from origin 'http://localhost:4200' has
been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is
present on the requested resource.
on the server side:
WARNING:tornado.access:404 GET /socket.io/?EIO=3&transport=polling&t=MSHp-m5
(127.0.0.1) 1.00ms
I have a server Written in Tornado written in python 2.7, here is the code:
from tornado import websocket, web, ioloop , httpserver
import json
import socketio
from urlparse import urlparse
cl =
class SocketHandler(websocket.WebSocketHandler):
def check_origin(self, origin):
print "check origin"
return True
def open(self):
if self not in cl:
cl.append(self)
self.write_message("connection opened")
def on_close(self):
if self in cl:
cl.remove(self)
print "connection closed"
def on_message(self, message):
print "Message received: ".format(message)
self.write_message("message received")
if __name__ == '__main__':
app = web.Application([
(r'/ws', SocketHandler),
# (r'/api', ApiHandler),
(r'/(favicon.ico)', web.StaticFileHandler, 'path': '../'),
(r'/(rest_api_example.png)', web.StaticFileHandler, 'path': './'),
], user_settings="websocket_allow_origin": "*")
app.listen(8888)
ioloop.IOLoop.instance().start()
I also have a client written in Angular 6 who uses socket.io to start a session with the server. the client at the moment run on localhost:4200 but in the future will run on a different machine then the server and will not be rendered by the server, means that i can't just run the client on the same port as the server because it doesn't fit my needs.
here is the angular service code from the which deals with the comunication to the server.:
import Injectable from '@angular/core';
import HttpClient from '@angular/common/http';
import Observable,BehaviorSubject from 'rxjs';
import delay,map,catchError from 'rxjs/operators';
import * as io from 'socket.io-client';
@Injectable(providedIn: 'root')
export class PysystemRunnerService
private PDB_WEB_PORT = "23456";
private editUrl: string = 'http://127.0.0.1:' + this.PDB_WEB_PORT +
'/l1commands/pysystem_runner_edit_list';
private socket;
private socket_port = "8888";
private socketUrl = "ws://127.0.0.1:" + this.socket_port + "/ws"
constructor(private http: HttpClient)
this.socket = io(this.socketUrl);
handleEdit(data)
var ans = this.http.post(this.editUrl, data).pipe(
delay(100),
map((data: any) => (data.data
handleRun(data)
///////////////socket logic////////////////
////////////////////////////
public getMessages = () =>
return Observable.create((observer) =>
this.socket.on('new-message', (message) =>
observer.next(message);
);
);
//handle http errors
private handleError(error: any)
let errMsg = (error.message) ? error.message :
error.status ? `$error.status - $error.statusText` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
I was reading the Tornado documantation and it says that making the check_origin function return True should solve the CORS issue but it didn't.
python angular socket.io cors tornado
Is there any reason that you use "socket.io" and not simply Angular's HttpClient? Besides, look directly at Http headers with Wireshark or any other tool, don't trust the documentation too much. Your client should sent the "Origin" header together with some other header(s), and this Origin is what your Tornado server should check to be valid (according to the CORS configuration on the Tornado server side).
– John Donn
Nov 14 '18 at 11:41
1
the main reason i use socket.io and not the simple anuglar's HttpClient is that i need to establish two ways communication, means i need the server to be able to push notifications to the client when an event occur on the server side
– Amit Biran
Nov 14 '18 at 11:53
add a comment |
I am trying to establish a comunication via socket.io socket between an angular 6 client and a tornado server. i get the followint errors:
on the client side:
Access to XMLHttpRequest at 'http://127.0.0.1:8888/socket.io/?
EIO=3&transport=polling&t=MSHp-m5' from origin 'http://localhost:4200' has
been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is
present on the requested resource.
on the server side:
WARNING:tornado.access:404 GET /socket.io/?EIO=3&transport=polling&t=MSHp-m5
(127.0.0.1) 1.00ms
I have a server Written in Tornado written in python 2.7, here is the code:
from tornado import websocket, web, ioloop , httpserver
import json
import socketio
from urlparse import urlparse
cl =
class SocketHandler(websocket.WebSocketHandler):
def check_origin(self, origin):
print "check origin"
return True
def open(self):
if self not in cl:
cl.append(self)
self.write_message("connection opened")
def on_close(self):
if self in cl:
cl.remove(self)
print "connection closed"
def on_message(self, message):
print "Message received: ".format(message)
self.write_message("message received")
if __name__ == '__main__':
app = web.Application([
(r'/ws', SocketHandler),
# (r'/api', ApiHandler),
(r'/(favicon.ico)', web.StaticFileHandler, 'path': '../'),
(r'/(rest_api_example.png)', web.StaticFileHandler, 'path': './'),
], user_settings="websocket_allow_origin": "*")
app.listen(8888)
ioloop.IOLoop.instance().start()
I also have a client written in Angular 6 who uses socket.io to start a session with the server. the client at the moment run on localhost:4200 but in the future will run on a different machine then the server and will not be rendered by the server, means that i can't just run the client on the same port as the server because it doesn't fit my needs.
here is the angular service code from the which deals with the comunication to the server.:
import Injectable from '@angular/core';
import HttpClient from '@angular/common/http';
import Observable,BehaviorSubject from 'rxjs';
import delay,map,catchError from 'rxjs/operators';
import * as io from 'socket.io-client';
@Injectable(providedIn: 'root')
export class PysystemRunnerService
private PDB_WEB_PORT = "23456";
private editUrl: string = 'http://127.0.0.1:' + this.PDB_WEB_PORT +
'/l1commands/pysystem_runner_edit_list';
private socket;
private socket_port = "8888";
private socketUrl = "ws://127.0.0.1:" + this.socket_port + "/ws"
constructor(private http: HttpClient)
this.socket = io(this.socketUrl);
handleEdit(data)
var ans = this.http.post(this.editUrl, data).pipe(
delay(100),
map((data: any) => (data.data
handleRun(data)
///////////////socket logic////////////////
////////////////////////////
public getMessages = () =>
return Observable.create((observer) =>
this.socket.on('new-message', (message) =>
observer.next(message);
);
);
//handle http errors
private handleError(error: any)
let errMsg = (error.message) ? error.message :
error.status ? `$error.status - $error.statusText` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
I was reading the Tornado documantation and it says that making the check_origin function return True should solve the CORS issue but it didn't.
python angular socket.io cors tornado
I am trying to establish a comunication via socket.io socket between an angular 6 client and a tornado server. i get the followint errors:
on the client side:
Access to XMLHttpRequest at 'http://127.0.0.1:8888/socket.io/?
EIO=3&transport=polling&t=MSHp-m5' from origin 'http://localhost:4200' has
been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is
present on the requested resource.
on the server side:
WARNING:tornado.access:404 GET /socket.io/?EIO=3&transport=polling&t=MSHp-m5
(127.0.0.1) 1.00ms
I have a server Written in Tornado written in python 2.7, here is the code:
from tornado import websocket, web, ioloop , httpserver
import json
import socketio
from urlparse import urlparse
cl =
class SocketHandler(websocket.WebSocketHandler):
def check_origin(self, origin):
print "check origin"
return True
def open(self):
if self not in cl:
cl.append(self)
self.write_message("connection opened")
def on_close(self):
if self in cl:
cl.remove(self)
print "connection closed"
def on_message(self, message):
print "Message received: ".format(message)
self.write_message("message received")
if __name__ == '__main__':
app = web.Application([
(r'/ws', SocketHandler),
# (r'/api', ApiHandler),
(r'/(favicon.ico)', web.StaticFileHandler, 'path': '../'),
(r'/(rest_api_example.png)', web.StaticFileHandler, 'path': './'),
], user_settings="websocket_allow_origin": "*")
app.listen(8888)
ioloop.IOLoop.instance().start()
I also have a client written in Angular 6 who uses socket.io to start a session with the server. the client at the moment run on localhost:4200 but in the future will run on a different machine then the server and will not be rendered by the server, means that i can't just run the client on the same port as the server because it doesn't fit my needs.
here is the angular service code from the which deals with the comunication to the server.:
import Injectable from '@angular/core';
import HttpClient from '@angular/common/http';
import Observable,BehaviorSubject from 'rxjs';
import delay,map,catchError from 'rxjs/operators';
import * as io from 'socket.io-client';
@Injectable(providedIn: 'root')
export class PysystemRunnerService
private PDB_WEB_PORT = "23456";
private editUrl: string = 'http://127.0.0.1:' + this.PDB_WEB_PORT +
'/l1commands/pysystem_runner_edit_list';
private socket;
private socket_port = "8888";
private socketUrl = "ws://127.0.0.1:" + this.socket_port + "/ws"
constructor(private http: HttpClient)
this.socket = io(this.socketUrl);
handleEdit(data)
var ans = this.http.post(this.editUrl, data).pipe(
delay(100),
map((data: any) => (data.data
handleRun(data)
///////////////socket logic////////////////
////////////////////////////
public getMessages = () =>
return Observable.create((observer) =>
this.socket.on('new-message', (message) =>
observer.next(message);
);
);
//handle http errors
private handleError(error: any)
let errMsg = (error.message) ? error.message :
error.status ? `$error.status - $error.statusText` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
I was reading the Tornado documantation and it says that making the check_origin function return True should solve the CORS issue but it didn't.
python angular socket.io cors tornado
python angular socket.io cors tornado
edited Nov 14 '18 at 11:00
Amit Biran
asked Nov 14 '18 at 10:34
Amit BiranAmit Biran
163
163
Is there any reason that you use "socket.io" and not simply Angular's HttpClient? Besides, look directly at Http headers with Wireshark or any other tool, don't trust the documentation too much. Your client should sent the "Origin" header together with some other header(s), and this Origin is what your Tornado server should check to be valid (according to the CORS configuration on the Tornado server side).
– John Donn
Nov 14 '18 at 11:41
1
the main reason i use socket.io and not the simple anuglar's HttpClient is that i need to establish two ways communication, means i need the server to be able to push notifications to the client when an event occur on the server side
– Amit Biran
Nov 14 '18 at 11:53
add a comment |
Is there any reason that you use "socket.io" and not simply Angular's HttpClient? Besides, look directly at Http headers with Wireshark or any other tool, don't trust the documentation too much. Your client should sent the "Origin" header together with some other header(s), and this Origin is what your Tornado server should check to be valid (according to the CORS configuration on the Tornado server side).
– John Donn
Nov 14 '18 at 11:41
1
the main reason i use socket.io and not the simple anuglar's HttpClient is that i need to establish two ways communication, means i need the server to be able to push notifications to the client when an event occur on the server side
– Amit Biran
Nov 14 '18 at 11:53
Is there any reason that you use "socket.io" and not simply Angular's HttpClient? Besides, look directly at Http headers with Wireshark or any other tool, don't trust the documentation too much. Your client should sent the "Origin" header together with some other header(s), and this Origin is what your Tornado server should check to be valid (according to the CORS configuration on the Tornado server side).
– John Donn
Nov 14 '18 at 11:41
Is there any reason that you use "socket.io" and not simply Angular's HttpClient? Besides, look directly at Http headers with Wireshark or any other tool, don't trust the documentation too much. Your client should sent the "Origin" header together with some other header(s), and this Origin is what your Tornado server should check to be valid (according to the CORS configuration on the Tornado server side).
– John Donn
Nov 14 '18 at 11:41
1
1
the main reason i use socket.io and not the simple anuglar's HttpClient is that i need to establish two ways communication, means i need the server to be able to push notifications to the client when an event occur on the server side
– Amit Biran
Nov 14 '18 at 11:53
the main reason i use socket.io and not the simple anuglar's HttpClient is that i need to establish two ways communication, means i need the server to be able to push notifications to the client when an event occur on the server side
– Amit Biran
Nov 14 '18 at 11:53
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298130%2fcors-error-on-comunication-between-angular-6-client-and-tornado-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298130%2fcors-error-on-comunication-between-angular-6-client-and-tornado-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is there any reason that you use "socket.io" and not simply Angular's HttpClient? Besides, look directly at Http headers with Wireshark or any other tool, don't trust the documentation too much. Your client should sent the "Origin" header together with some other header(s), and this Origin is what your Tornado server should check to be valid (according to the CORS configuration on the Tornado server side).
– John Donn
Nov 14 '18 at 11:41
1
the main reason i use socket.io and not the simple anuglar's HttpClient is that i need to establish two ways communication, means i need the server to be able to push notifications to the client when an event occur on the server side
– Amit Biran
Nov 14 '18 at 11:53