Has*_*Ali 0 python sockets proxy
我正在尝试设置代理,并且正在使用socket和httlib模块。我的Web浏览器指向运行服务器的localhost和端口,并且正在通过服务器处理HTTP请求。当浏览器正在请求网页时,我从HTTP标头中提取网址,然后尝试以以下方式通过代理进行请求:
conn = httplib.HTTPSConnection(url,serverPort)
conn.request("GET",url)
r1 = conn.getresponse()
print r1.status,r1.reason
Run Code Online (Sandbox Code Playgroud)
请注意,第一行中的serverPort参数是代理所在的端口,而url是当请求网页时从浏览器接收到的HTTP标头中提取的url。
因此,当我运行代理并在浏览器中输入诸如http://www.google.com或http://www.getmetal.org之类的地址时,似乎出现了错误。
错误是:
socket.gaierror:[Errno 8]提供的节点名或服务名,或者未知
还有痕迹:-
http://i.stack.imgur.com/UgZwD.png
如果有人对问题可能有什么建议,我将非常高兴。这是代理服务器的代码:注意:如果正在测试,由于可能必须将所有4个空格放在右边以使其显示为代码段,因此可能存在一些缩进问题
from socket import *
import httplib
import webbrowser
import string
serverPort = 2000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(2)
urlList=['www.facebook.com','www.youtube.com','www.twitter.com']
print 'The server is ready to receive'
while 1:
connectionSocket, addr = serverSocket.accept()
print addr
req= connectionSocket.recv(1024)
#parse Get request here abnd extract url
reqHeaderData= req.split('\n')
newList=[]
x=0
while x<len(reqHeaderData):
st=reqHeaderData[x]
element= st.split(' ')
print element
newList.append(element)
x=x+1
print newList[0][1]
url = newList[0][1]
url= url[:-1]
for i in urlList:
if url ==i:
raise Exception("The website you are trying to access is blocked")
connectionSocket.send('Valid')
print(url)
conn = httplib.HTTPSConnection(url,serverPort)
print conn
conn.request("GET",url)
print "request printed"
r1 = conn.getresponse()
print r1.status,r1.reason
print r1
#200 OK
data = r1.read()
x= r1.getheaders()
for i in x:
print i
connectionSocket.close()
Run Code Online (Sandbox Code Playgroud)
小智 5
我看到这是一个常见的错误...
url="https://foo.tld"
port=443
conn=httplib.HTTPConnection(url,port)
Run Code Online (Sandbox Code Playgroud)
由于“ https://”,因此无法使用...
您应该这样做:
url="foo.tld"
port=443
conn=httplib.HTTPConnection(url,port)
Run Code Online (Sandbox Code Playgroud)
这会起作用。我不确定这是否是您的特定问题,但肯定可以验证。
| 归档时间: |
|
| 查看次数: |
4517 次 |
| 最近记录: |