Setting up Proxy with Python3 - python-3.x

I have a question and my classmate can't fix it either:
how to set up a proxy using requests module?
I think it is easy and I can fix it very quickly. I can use :
proxy = {
'http':'http://74.125.204.103' #just an example
}
and
requests.get(www.youtube.com,proxies = proxy)
And we thought it will connect with 74.125.204.103
But WE ARE WRONG!
It still connects with my own IP address. We use youtube and connect on on video but the times watched is not changing. we also use grabify and IT's still the same.So how can I set up the proxy in other ways?

I believe you're getting this because you only have the proxy specified for HTTP and youtube will redirect you to HTTPS at which point requests has no proxy information.
proxies = {
'http':'http://proxy:port',
'https':'http://proxy:port'
}
Try adding the line for the https schema.

Related

NetSuite HTTP POST Request to a Local IP Printer

Ive been trying to send a http post request using a client script to send a zpl string to our local ip printer. I keep getting "The host you requested null is unknown or cannot be found."
What am I doing wrong? Do I need to use a RESTlet?
I can also do POST prints with this printer outside of netsuite, the problem is through netsuite.
function print(tag) {
var zpl = "^XA^CF0,30^FO20,20^FD" + tag.company + "^FS^FO20,50^FDPO # " + tag.poNum + "^FS^FO325,50^FDORD # " + tag.ordNum + "^FS^FO630,50^FDQTY^FS^CF0,50^FO700,40^FD" + tag.quantity + "^FS^BY2,10,100^FO" + calculateBarcodeDistance(tag.item.length) + ",175^BY3^BCN,50,Y,N,N,N^FD" + tag.item + "^FS^XZ";
var printUrl = "http://192.168.0.0/pstprnt";
var response = http.request({
method: http.Method.POST,
url: printUrl,
body: zpl
});
}
According to SuiteAnswers #44853 the N/http module is only supported in server side scripts:
So you would probably have to use a suitelet and call it from your client. Unfortunately, this would mean you have to expose a port on your public IP address and port-forward from there to the printer.
Alternatively, you could use the fetch API as suggested by Jon in comments, or an XMLHttpRequest, to achieve the same thing through the browser.
About the easiest thing to do would be to run ngrok on a box that has access to the internet. You’d post to the public address and that would forward to you printer. There’s a way to set a credential on that so you don’t get random print jobs from port scanners.
Then you’d use your current sample to send to a remote https address and that would send to ngrok on your server which would forward to your printer
You would need to create a simple Suitelet that uses the N/http module. So your client script would post to the printer suitelet, which would make the actual post and then return the data back to your client script.
You can use jQuery.post(...) to in your client script.
Another "easy" way to do this would be to install apache/nginx/other https capable server on the machine you are generating your labels on.
You'd map some useful name to one of the loop back addresses
You'd generate a self signed certificate for the host name above and have your server use that host and listen on the loop back address.
You'd configure the server as a reverse proxy to forward requests to the printer's IP address
You could then use send print requests to the local server (Use an iframe post hack) and though it's more work than the ngrok solution there's no direct cost associated.
I would say that in order to do this you need to POST to a server with a static IP address whereas your ip address in the example looks like an ip produced by a home router aka a dynamic ip.
The first line in the Netsuite help for the http module states that it can be used in client and server scripts. I have successfully sent a POST request via the http module to a http server and got a success response back. I did not have to use https.
You can test this by changing the url to http://httpbin.org:80/post, they provide a service that just sends whatever you post back to you.

Why is X-Forwarded-For header not getting set by node-express proxy?

I have a single node file which does the following:
It listens on two ports: 80 and 443 (for https).
It redirects connections on 80 to 443.
And on 443 is a reverse proxy that does a round-robin redirects to several local servers over plain http.
The problem that I have is that in the actual target servers, I am unable to get the actual remote IP address of the browser.
I get the address of the reverse proxy.
The request is made by the reverse proxy, so thats expected I guess.
So, I did the following in the reverse proxy (only relevant code lines shown):
proxy = httpProxy.createServer();
var https_app = express();
https.createServer(sslCerts, https_app).listen(443, function () {
...
});
https_app.all("/",function(req, res) {
...
//res.append('X-Forwarded-For',req.connection.remoteAddress);
proxy.web(req,res, {target: local_server});
...
}
I need to do something like res.append('X-Forwarded-For',req.connection.remoteAddress) to the proxy server.. in its request header. The issue of setting the address is secondary. I first need to set the header itself which can be read by the target server. The proxy itself does not set this header, which I think it should by default. Or should it? Or does it and I am doing something wrong to read it?
I cannot see your definition of proxy, but I'm assuming it is same as the following code, using express-http-proxy. I expect this alternative method will work for you:
var proxy = require('express-http-proxy');
var myProxy= proxy('localhost:443', {
forwardPath: function (req, res) {
return require('url').parse(req.url).path;
}
});
And then simply:
app.use("/*", myProxy);
I hope this helps.

Reverse proxy for a NodeJS website

I look for a reverse proxy for my NodeJS website. I thought of Varnish or nginx or something else.
What would you suggest me and why (do not necessarily focus on Varnish vs nginx)?
nginx would probably be the best stand-alone solution, however, when I'm working with Node.js, I prefer to keep everything in Node.js so I don't have to worry about the (relatively simple) configuration. I personally use node-reverse-proxy, which allows me to just specify some simple routes in a simple application, and then route it back to the individual applications.
This is the node-reverse-proxy sample code:
var node_reverse_proxy = require('node-reverse-proxy');
var ip = '127.0.0.1';
var reverse_proxy = new node_reverse_proxy({
'first_host.com' : ip + ':8082',
'my.second_host.com' : ip + ':8081',
'my.second_host.com/page/' : ip + ':8080',
'' : ip + ':8080' // catch all other routes
});
reverse_proxy.start(80);
You might find that nginx better suits your needs, but personally, for a simple reverse proxy setup, I do prefer node-reverse-proxy.

Meteor: How to get the hostname, server side

On the client I can use window.location.hostname to get the hostname. How can I get the same on the server?
I need this to work behind an Apache proxy, unfortunately Meteor.absoluteUrl() gives me localhost:3000. I also want it to work for different domains, I want one Meteor app that gives different results for different domains.
This question is somewhat related: Get hostname of current request in node.js Express
Meteor.absoluteUrl() given that your ROOT_URL env variable is set correctly.
See the following docs: http://docs.meteor.com/#meteor_absoluteurl.
Meteor doesn't know the outside-facing address of the proxy that it's sitting behind, and the (virtual) domain that this proxy was accessed by would have to be forwarded to the Meteor app for it to do what you are asking for. I don't think this is currently supported.
According to this you can now get the Host header inside Meteor.publish() and Meteor.methods() calls by accessing:
this.connection.httpHeaders.host
Elsewhere in the application, it's probably difficult to determine the Host header that is being used to connect.
If you want the hostname of the server, as configured in /etc/hostname for example:
With meteorite:
$ mrt add npm
In your server code:
os = Npm.require('os')
hostname = os.hostname()
This has no connection to the Host header provided in the incoming request.
updated answer with some of chmac's words from the comment below
In any server-side meteor file you can add:
if (Meteor.isServer) {
Meteor.onConnection(function(result){
var hostname = result.httpHeaders.referer; //This returns http://foo.example.com
});
}
You can fetch host as EnvironmentVariable from DDP object in method and publication. Meteor accounts-base package fetch userId via this way.
const currentDomain = function() {
const currentInvocation = DDP._CurrentMethodInvocation.get() || DDP._CurrentPublicationInvocation.get();
return currentInvocation.connection.httpHeaders.host;
}

Websocket server running fine but cannot connect from client (what url should I use?)

OK this is very simple to anyone who's used websocket and nodejs.
I have created a websocket server named ws_server.js and put it in C:\Program Files (x86)\nodejs where I have installed the nodejs framework. I started the server and it is running and it says it's listening on port 8080. So far so good, I have the server running.
Now I simply want to connect to it from client code so that I can do all that lovely stuff about capturing events using event listeners etc. The problem is, embarassingly, I cannot figure out what URL to use to connect to my websocket server.
function init() {
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket("ws://localhost:8080/"); // WHAT URL SHOULD BE USED HERE?
websocket.onopen = function(evt) { alert("OPEN") };
websocket.onclose = function(evt) { alert("CLOSE") };
websocket.onmessage = function(evt) { alert("MESSAGE") };
websocket.onerror = function(evt) { alert("ERROR") };
}
function doSend(message) {
// this would be called by user pressing a button somewhere
websocket.send(message);
alert("SENT");
}
window.addEventListener("load", init, false);
When I use ws://localhost:8080 the only events that trigger are CLOSE and ERROR. I cannot get the client to connect. I must be missing something very simple. Do I need to set up my nodejs folder in IIS for example and then use that as the URL?
Just to reiterate, the websocket server is running fine, I just don't know what URL to use to connect to it from the client.
EDIT: The websocket server reports the following error.
Specified protocol was not requested by the client.
I think I have got it working by doing the following.
var websocket = new WebSocket("ws://localhost:8080/","echo-protocol");
The problem being that I needed to specify a protocol. At least now I get the onopen event. ...if nothing much else
I was seeing the same error, the entire web server goes down. Adding the protocol fixes it but leaves me wondering why it was implemented this way. I mean, one bad request should not bring down your server.
You definitely have to encase it a try/catch, but the example code provided here https://www.npmjs.com/package/websocket (2019-08-07) does not. This issue can be easily avoided.
I just wanted to share a crazy issue that I had. I was able to connect to a websocket of an old version of a 3rd party app in one computer, but not to a newer version of the app in another.
Moreever, even in new computer with the new version of the app, The app was able to connect to the websocket, but no matter what I did, when I tried to connect with my own code, I kept getting the error message that the websocket connection failed
Long story short, They changed an apache configuration that allowed connecting to the websocket via a proxy.
In the old version, apache config was:
ProxyPass /socket/ ws://localhost:33015/ retry=10
ProxyPass /socket ws://localhost:33015/ retry=10
In the new version, apache config was changed to:
ProxyPass /socket/ ws://localhost:33015/ retry=10
By bad luck, I was trying to connect to ws://localhost/socket and not to ws://localhost/socket/. As a result, proxy was not found, and connection returned an error.
Moral of the story: Make sure that you are trying to connect to a websocket url that exists.
For me, the solution was to change the URL from ws:// to wss://. This is because the server I was connecting to had updated its security, and now only accepted wss.

Resources