Skip to content

Instantly share code, notes, and snippets.

@MichelleDalalJian
Created October 7, 2017 14:53
Show Gist options
  • Save MichelleDalalJian/f89c989e825f092b0ca428c390a6edb1 to your computer and use it in GitHub Desktop.
Save MichelleDalalJian/f89c989e825f092b0ca428c390a6edb1 to your computer and use it in GitHub Desktop.
Exploring the HyperText Transport Protocol You are to retrieve the following document using the HTTP protocol in a way that you can examine the HTTP Response headers. http://data.pr4e.org/intro-short.txt There are three ways that you might retrieve this web page and look at the response headers: Preferred: Modify the socket1.py program to retrie…
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)
while True:
data = mysock.recv(512)
if (len(data) < 1):
break
print(data.decode())
mysock.close()
@snehalawasthi
Copy link

i followed this process but got a traceback in line 1 and line 3

@Poojithanallana99
Copy link

i followed this process but got a traceback in line 1 and line 3

yes, even i got the same problem
can someone solve the issue??

@mxxene13
Copy link

just copy the code given by dr. Chuck, and replace the URL next to GET with the URL of the text that we need to analize.

Or just use the java developer view from your chrome navigator and copy the values.

Copy link

ghost commented Aug 1, 2020

Last-Modified:
Sat, 13 May 2017 11:22:22 GMT

ETag:
1d3-54f6609240717

Content-Length:
467

Cache-Control:
max-age=0, no-cache, no-store, must-revalidate

Content-Type:
text/plain

@lokendratanwar27
Copy link

just copy the code given by dr. Chuck, and replace the URL next to GET with the URL of the text that we need to analize.

Or just use the java developer view from your chrome navigator and copy the values.

can you please tell me exactly how to run command prompt in windows 10 as windows does not provide telnet by default, we have to enable it manually. I want the steps of what to write in command promt.

@som983
Copy link

som983 commented Sep 7, 2020

if u want to run this program on cmd go to the folder where the file is located and open cmd from that folder by clicking on adress bar and typing cmd there.Then run that program by python "name of your file".py.
That should do.

@som983
Copy link

som983 commented Sep 7, 2020

net

@SudaYugandhar
Copy link

Command Prompt 15-09-2020 20_21_40

socket1 py — C__Users_SUDA YUGANDHAR_Desktop_Python — Atom 15-09-2020 20_23_02

Can someone tell me why is it showing "Bad Request"?

@28GREKA
Copy link

28GREKA commented Oct 22, 2020

image
You have to copy-paste the code, in my case i'am using visual studio Code and then you change when says GET and put the link that is in your assignament just like this and put the answers:)

@sonai1504
Copy link

if u want to run this program on cmd go to the folder where the file is located and open cmd from that folder by clicking on adress bar and typing cmd there.Then run that program by python "name of your file".py.
That should do.
C:\Users\User\PycharmProjects\pythonProject>python socket.py
C:\Users\User\PycharmProjects\pythonProject>py socket.py
Traceback (most recent call last):
File "socket.py", line 1, in
import socket
File "C:\Users\User\PycharmProjects\pythonProject\socket.py", line 3, in
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AttributeError: partially initialized module 'socket' has no attribute 'AF_INET' (most likely due to a circular import)

i am getting this error if i am following these steps and if i am writing 'telnet data.pr4e.org 80' then pasting the URL given in the assignment it is showing:
HTTP/1.1 400 Bad Request
Date: Sat, 31 Oct 2020 18:45:33 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 308
Connection: close
Content-Type: text/html; charset=iso-8859-1

                                              <html><head>
                                                          <title>400 Bad Request</title>
                                                                                        </head><body>
                                                                                                     <h1>Bad Request</h1>
 <p>Your browser sent a request that this server could not understand.<br />
                                                                            </p>
                                                                                <hr>
                                                                                    <address>Apache/2.4.18 (Ubuntu) Server at do1.dr-chuck.com Port 80</address>
                                        </body></html>

Connection to host lost.

please help me.

@Yerkonite
Copy link

i followed this process but got a traceback in line 1 and line 3

The same here. Have you found what was causing these tracebacks?

@Immaculate75
Copy link

Last-Modified:
Sat, 13 May 2017 11:22:22 GMT
ETag:
1d3-54f6609240717
Content-Length:
467
Cache-Control:
max-age=0, no-cache, no-store, must-revalidate
Content-Type:
text/plain

This is working.

@Eduard0Hdz
Copy link

Last-Modified:
Sat, 13 May 2017 11:22:22 GMT
ETag:
1d3-54f6609240717
Content-Length:
467
Cache-Control:
max-age=0, no-cache, no-store, must-revalidate
Content-Type:
text/plain

This is working.

@takenbydonuts
Copy link

1

THIS IS WORKING:
Last-Modified:
Sat, 13 May 2017 11:22:22 GMT
ETag:
1d3-54f6609240717
Content-Length:
467
Cache-Control:
max-age=0, no-cache, no-store, must-revalidate
Content-Type:
text/plain

@maulya230
Copy link

maulya230 commented Dec 27, 2021

Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Sat, 13 May 2017 11:22:22 GMT
ETag: "1d3-54f6609240717"
Accept-Ranges: bytes
Content-Length: 467
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Connection: close
Content-Type: text/plain

@youneshamouddo
Copy link

thank you

@tokyohmachine
Copy link

This is very simple. First, put the code inside pycharm, you need only change the - Preferred: Modify the [socket1.py]
The code given in coursera:
image

Code in Pycharm:
image

Then RUN, you will get the answer for the task.

@jjtlemall
Copy link

wonderful!

@mde-2590
Copy link

mde-2590 commented Aug 15, 2022

what is cache control??

If you run the code in what ever IDE (Pycharm for example) you are using you will see:

Last-Modified: Sat, 13 May 2017 11:22:22 GMT
ETag: "1d3-54f6609240717"
Accept-Ranges: bytes
Content-Length: 467
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Connection: close
Content-Type: text/plain

Why should you learn to write programs?

Writing programs (or programming) is a very creative
and rewarding activity. You can write programs for
many reasons, ranging from making your living to solving
a difficult data analysis problem to having fun to helping
someone else solve a problem. This book assumes that
everyone needs to know how to program, and that once
you know how to program you will figure out what you want
to do with your newfound skills.

Process finished with exit code 0

Ensure you are highlighting the code if you have not saved it otherwise it will exit with a code 0

@FahadJamshaid
Copy link

This is very simple. First, put the code inside pycharm, you need only change the - Preferred: Modify the [socket1.py] The code given in coursera: image

Code in Pycharm: image

Then RUN, you will get the answer for the task.

Hi. With the same code running in Bracket text editor I am getting error in cmd. Can anyone please help
6 6 23

@tahira2k16
Copy link

  1. save it as newsocket.py in any other directory, not in which you're currently working.
  2. problem
    image

solution
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment