nodename nor servname provided, or not known on tcp connection in Python
I am tring to understand the socket connections in python and everytime i am tring to connect to a url it's giving me this error:
nodename nor servname provided, or not known
which i have no idea why? And sometimes it's only showing 301
and never a 200
status!
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "Whatever url i am giving"
server_address = (host, 80)
request_header = request_header = 'GET / HTTP/1.1rnHost: '+host+'rnrn'
try:
s.connect(server_address)
s.send(request_header.encode('utf-8'))
result = s.recv(4096).decode('utf-8')
while (len(result) > 0):
print(result)
result = s.recv(4096)
except Exception as ex:
print("Unexpected error:", ex)
s.close()
I know there are other questions but that doesn't satisfy my query. Can someone point me out what's happening here??
python-3.x sockets
add a comment |
I am tring to understand the socket connections in python and everytime i am tring to connect to a url it's giving me this error:
nodename nor servname provided, or not known
which i have no idea why? And sometimes it's only showing 301
and never a 200
status!
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "Whatever url i am giving"
server_address = (host, 80)
request_header = request_header = 'GET / HTTP/1.1rnHost: '+host+'rnrn'
try:
s.connect(server_address)
s.send(request_header.encode('utf-8'))
result = s.recv(4096).decode('utf-8')
while (len(result) > 0):
print(result)
result = s.recv(4096)
except Exception as ex:
print("Unexpected error:", ex)
s.close()
I know there are other questions but that doesn't satisfy my query. Can someone point me out what's happening here??
python-3.x sockets
add a comment |
I am tring to understand the socket connections in python and everytime i am tring to connect to a url it's giving me this error:
nodename nor servname provided, or not known
which i have no idea why? And sometimes it's only showing 301
and never a 200
status!
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "Whatever url i am giving"
server_address = (host, 80)
request_header = request_header = 'GET / HTTP/1.1rnHost: '+host+'rnrn'
try:
s.connect(server_address)
s.send(request_header.encode('utf-8'))
result = s.recv(4096).decode('utf-8')
while (len(result) > 0):
print(result)
result = s.recv(4096)
except Exception as ex:
print("Unexpected error:", ex)
s.close()
I know there are other questions but that doesn't satisfy my query. Can someone point me out what's happening here??
python-3.x sockets
I am tring to understand the socket connections in python and everytime i am tring to connect to a url it's giving me this error:
nodename nor servname provided, or not known
which i have no idea why? And sometimes it's only showing 301
and never a 200
status!
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "Whatever url i am giving"
server_address = (host, 80)
request_header = request_header = 'GET / HTTP/1.1rnHost: '+host+'rnrn'
try:
s.connect(server_address)
s.send(request_header.encode('utf-8'))
result = s.recv(4096).decode('utf-8')
while (len(result) > 0):
print(result)
result = s.recv(4096)
except Exception as ex:
print("Unexpected error:", ex)
s.close()
I know there are other questions but that doesn't satisfy my query. Can someone point me out what's happening here??
python-3.x sockets
python-3.x sockets
asked Nov 18 '18 at 23:43
Fay007Fay007
1,04611242
1,04611242
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You don't connect to a URL. You connect to a host. When I assign host = stackoverflow.com
, for example, your code works fine.
The socket layer itself knows nothing about URLs. A URL includes the path you supply to the host's HTTP server after you've connected. So, if you wish to retrieve, say, the URL "http://stackoverflow.com/questions", you connect to the host "stackoverflow.com", then provide this as the first line in the HTTP request:
GET /questions HTTP/1.1rn
This request (to stackoverflow.com) will in fact deliver a 301 response. 301 is a redirect response, telling you that the document you seek is available from a different host or service. This is an increasingly common response as most "http" sites now redirect the client to their corresponding "https" service.
If the host name you provide is not a valid hostname (for example, if you attempt to connect to "szackoverflow.com"), the hostname lookup that's being done automatically on your behalf will fail, resulting in a socket.gaierror
exception ("gai" => getaddrinfo
). On my linux system, that looks like this:
Unexpected error: [Errno -2] Name or service not known
On a different operating system, the text provided with that error might be worded differently.
yeah i actually figured that out already. Thanks for the answer.
– Fay007
Nov 22 '18 at 14:50
add a comment |
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%2f53366587%2fnodename-nor-servname-provided-or-not-known-on-tcp-connection-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't connect to a URL. You connect to a host. When I assign host = stackoverflow.com
, for example, your code works fine.
The socket layer itself knows nothing about URLs. A URL includes the path you supply to the host's HTTP server after you've connected. So, if you wish to retrieve, say, the URL "http://stackoverflow.com/questions", you connect to the host "stackoverflow.com", then provide this as the first line in the HTTP request:
GET /questions HTTP/1.1rn
This request (to stackoverflow.com) will in fact deliver a 301 response. 301 is a redirect response, telling you that the document you seek is available from a different host or service. This is an increasingly common response as most "http" sites now redirect the client to their corresponding "https" service.
If the host name you provide is not a valid hostname (for example, if you attempt to connect to "szackoverflow.com"), the hostname lookup that's being done automatically on your behalf will fail, resulting in a socket.gaierror
exception ("gai" => getaddrinfo
). On my linux system, that looks like this:
Unexpected error: [Errno -2] Name or service not known
On a different operating system, the text provided with that error might be worded differently.
yeah i actually figured that out already. Thanks for the answer.
– Fay007
Nov 22 '18 at 14:50
add a comment |
You don't connect to a URL. You connect to a host. When I assign host = stackoverflow.com
, for example, your code works fine.
The socket layer itself knows nothing about URLs. A URL includes the path you supply to the host's HTTP server after you've connected. So, if you wish to retrieve, say, the URL "http://stackoverflow.com/questions", you connect to the host "stackoverflow.com", then provide this as the first line in the HTTP request:
GET /questions HTTP/1.1rn
This request (to stackoverflow.com) will in fact deliver a 301 response. 301 is a redirect response, telling you that the document you seek is available from a different host or service. This is an increasingly common response as most "http" sites now redirect the client to their corresponding "https" service.
If the host name you provide is not a valid hostname (for example, if you attempt to connect to "szackoverflow.com"), the hostname lookup that's being done automatically on your behalf will fail, resulting in a socket.gaierror
exception ("gai" => getaddrinfo
). On my linux system, that looks like this:
Unexpected error: [Errno -2] Name or service not known
On a different operating system, the text provided with that error might be worded differently.
yeah i actually figured that out already. Thanks for the answer.
– Fay007
Nov 22 '18 at 14:50
add a comment |
You don't connect to a URL. You connect to a host. When I assign host = stackoverflow.com
, for example, your code works fine.
The socket layer itself knows nothing about URLs. A URL includes the path you supply to the host's HTTP server after you've connected. So, if you wish to retrieve, say, the URL "http://stackoverflow.com/questions", you connect to the host "stackoverflow.com", then provide this as the first line in the HTTP request:
GET /questions HTTP/1.1rn
This request (to stackoverflow.com) will in fact deliver a 301 response. 301 is a redirect response, telling you that the document you seek is available from a different host or service. This is an increasingly common response as most "http" sites now redirect the client to their corresponding "https" service.
If the host name you provide is not a valid hostname (for example, if you attempt to connect to "szackoverflow.com"), the hostname lookup that's being done automatically on your behalf will fail, resulting in a socket.gaierror
exception ("gai" => getaddrinfo
). On my linux system, that looks like this:
Unexpected error: [Errno -2] Name or service not known
On a different operating system, the text provided with that error might be worded differently.
You don't connect to a URL. You connect to a host. When I assign host = stackoverflow.com
, for example, your code works fine.
The socket layer itself knows nothing about URLs. A URL includes the path you supply to the host's HTTP server after you've connected. So, if you wish to retrieve, say, the URL "http://stackoverflow.com/questions", you connect to the host "stackoverflow.com", then provide this as the first line in the HTTP request:
GET /questions HTTP/1.1rn
This request (to stackoverflow.com) will in fact deliver a 301 response. 301 is a redirect response, telling you that the document you seek is available from a different host or service. This is an increasingly common response as most "http" sites now redirect the client to their corresponding "https" service.
If the host name you provide is not a valid hostname (for example, if you attempt to connect to "szackoverflow.com"), the hostname lookup that's being done automatically on your behalf will fail, resulting in a socket.gaierror
exception ("gai" => getaddrinfo
). On my linux system, that looks like this:
Unexpected error: [Errno -2] Name or service not known
On a different operating system, the text provided with that error might be worded differently.
answered Nov 19 '18 at 17:19
Gil HamiltonGil Hamilton
8,9191739
8,9191739
yeah i actually figured that out already. Thanks for the answer.
– Fay007
Nov 22 '18 at 14:50
add a comment |
yeah i actually figured that out already. Thanks for the answer.
– Fay007
Nov 22 '18 at 14:50
yeah i actually figured that out already. Thanks for the answer.
– Fay007
Nov 22 '18 at 14:50
yeah i actually figured that out already. Thanks for the answer.
– Fay007
Nov 22 '18 at 14:50
add a comment |
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%2f53366587%2fnodename-nor-servname-provided-or-not-known-on-tcp-connection-in-python%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