Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Pilot Lvl 1
Message 1 of 5

'GET ' concatenation

Hey everyone

After hours of searching and trying, I can't get this simple line to work. No matter what I did,I can't seem to be able to concatenate GET with a URL

I'm trying to solve this exercise : 

"Change the socket program socket1.py to prompt the user
for the URL so it can read any web page. You can use split('/') to
break the URL into its component parts so you can extract the host
name for the socket connect call."

 

Here's what I've written :

import socket

user_url = input("Enter url: ")
host_name = user_url.split("/")[2]
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((host_name, 80))
cmd = 'GET' + user_url + 'HTTP/1.0\r\n\r\n'
cmd = cmd.encode()
mysock.send(cmd)

It gives me the '400' bad request error. Any ideas ?

 

 

 

4 Replies
Highlighted
Pilot Lvl 2
Message 2 of 5

Re: 'GET ' concatenation

Looks to me like you are missing the necessary space on either side of 'user_url'.

 

I'd add the line 

print(cmd)

to see what you've actually got, I think right now you have (e.g.):

GEThttp://google.com/blahHTTP 1.0

when you need:

GET http://google.com/blah HTTP 1.0

 

Pilot Lvl 1
Message 3 of 5

Re: 'GET ' concatenation

Thanks for your reply

OK so I added a space after GET ( 'GET ') and also before HTTP (' HTTP') 

import socket

user_url = input("Enter url: ")
host_name = user_url.split("/")[2]
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((host_name, 80))
cmd = 'GET ' + user_url + ' HTTP/1.0\r\n\r\n'
cmd = cmd.encode()
print(cmd)
mysock.send(cmd)

while True:
data = mysock.recv(512)
if (len(data) < 1):
break
print(data.decode(),end='')

mysock.close()

And withprint(cmd), I get back this :

Enter url: https://www.py4e.com/code3/romeo.txt
b'GET https://www.py4e.com/code3/romeo.txt HTTP/1.0\r\n\r\n'
HTTP/1.1 400 Bad Request
Server: httppd
Mime-Version: 1.0
Date: Wed, 16 Jan 2019 11:21:59 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 1285
Connection: close

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ERROR: The requested URL could not be retrieved</title>
<style type="text/css"><!--
body {
margin: 0;
padding: 0;
background: #efefef;
font-family: verdana, sans-serif;
font-size: 12px;
color: #1e1e1e;
}
#titles {
padding-left: 10px;
}
h1, h2 {
color: #000000;
}
hr {
height: 1px;
border: none;
background: #808080;
margin: 0;
}
#content {
padding: 10px;
background: #ffffff;
}
pre {
font-family: sans-serif;
}
#footer {
font-size: 9px;
padding-left: 10px;
}

--></style>
</head><body>
<div id="titles">
<h1>ERROR</h1>
<h2>The requested URL could not be retrieved</h2>
</div>
<hr>
<div id="content">
<p>The following error was encountered while trying to retrieve the URL: <a href="https://www.py4e.com/code3/romeo.txt">https://www.py4e.com/code3/romeo.txt</a></p>
<blockquote id="error">
<p><b>Unsupported Request Method and Protocol</b></p>
</blockquote>
<p>Cache does not support all request methods for all access protocols. For example, you can not POST a Gopher request.</p>
<p>Your cache administrator is <a href="mailto:webmaster">webmaster</a>.</p>
<br>
</div>
<hr>
<div id="footer">
<p>Generated Wed, 16 Jan 2019 11:21:59 GMT</p>
</div>
</body></html>

 

 

Pilot Lvl 1
Message 4 of 5

Re: 'GET ' concatenation

I think I figured it out. The reason it didn't work was every link I posted was https instead of http. It works fine with all links starting with http

 

Commander Lvl 2
Message 5 of 5

Re: 'GET ' concatenation

Glad you fixed the problem and thanks for sharing your solution with the community!

 

@fire-eggs thanks for the help!


- Mark