2017-08-26 6 views
1
# -*- coding: utf-8 -*- 
""" 
Created on Sat Aug 26 17:31:06 2017 

@author: Pavan Vallapureddy 
""" 
""" 
Write a program 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. 
""" 

import socket 

url = input("Enter url: ") 
port = int(input("Enter port: ")) 
urlSplit = url.split("/") 
host = urlSplit[2] 

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
mysock.connect((host, port)) 
cmd = "GET " + url + " 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() 

のURLを入力します。 CMD = + "GET" URL + "HTTP/1.0 \ rをする\ n個の\ rをする\ n" .encode
例外TypeError:あなたは、文字列のインスタンスcmd方法encode()を呼び出す必要がありトレースバック

答えて

0

をbuiltin_function_or_methodない、をstrする必要があります。

cmd = "GET " + url + " HTTP/1.0\r\n\r\n" 
mysock.send(cmd.encode()) 
+0

です。あなたの助けをありがとう –

+0

@PavanVallapureddy助けてよかった!この回答を受け入れることを検討してください。ありがとう! – Ajax1234

関連する問題