gpt4 book ai didi

python - HTTP 代理服务器 [Python]

转载 作者:可可西里 更新时间:2023-11-01 16:42:06 29 4
gpt4 key购买 nike

我这里有一个有效的 HTTP 代理服务器的代码。如何创建另一个名为“Client”的程序?客户端会向多个网络服务器发送 HTTP GET 请求通过代理服务器。客户端程序连接到代理并向以下 3 个网站发送 HTTP GET 请求:(www.google.com、www.yahoo.com、www.stackoverflow.com)间隔 30 秒。

-我的总体问题是如何从 python 而不是我的网络浏览器向代理服务器发送 HTTP GET 请求?

操作系统 10.10.3 Python 3.4

当我在我的终端中调用这个代理时:

python 1869.py 2000

您可以提供任何端口号代替 2000。

输出:

starting server ....
Initiating server...
Accepting connection

然后在我的浏览器中(我使用的是最新版本的 chrome)我输入:

localhost:2000/www.stackoverflow.com 

我的终端输出是:

request is  GET  to URL :  www.stackoverflow.com
/www.stackoverflow.com
File Present in Cache

HTTP/1.1 301 Moved Permanently

Content-Type: text/html; charset=UTF-8

Location: http://stackoverflow.com/

Date: Thu, 07 May 2015 17:45:40 GMT

Content-Length: 148

Connection: close

Age: 0



<head><title>Document Moved</title></head>

<body><h1>Object Moved</h1>This document may be found <a HREF="http://stackoverflow.com/">here</a></body>
Reading file from cache

Initiating server...
Accepting connection

代理代码:

import socket
import sys
if len(sys.argv) <= 1:
print 'Usage: "python S.py port"\n[port : It is the port of the Proxy Server'
sys.exit(2)

# Server socket created, bound and starting to listen
Serv_Port = int(sys.argv[1]) # sys.argv[1] is the port number entered by the user
Serv_Sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket.socket function creates a socket.

# Prepare a server socket
print "starting server ...."
Serv_Sock.bind(('', Serv_Port))
Serv_Sock.listen(5)


def caching_object(splitMessage, Cli_Sock):
#this method is responsible for caching
Req_Type = splitMessage[0]
Req_path = splitMessage[1]
Req_path = Req_path[1:]
print "Request is ", Req_Type, " to URL : ", Req_path

#Searching available cache if file exists
file_to_use = "/" + Req_path
print file_to_use
try:
file = open(file_to_use[1:], "r")
data = file.readlines()
print "File Present in Cache\n"

#Proxy Server Will Send A Response Message
#Cli_Sock.send("HTTP/1.0 200 OK\r\n")
#Cli_Sock.send("Content-Type:text/html")
#Cli_Sock.send("\r\n")

#Proxy Server Will Send Data
for i in range(0, len(data)):
print (data[i])
Cli_Sock.send(data[i])
print "Reading file from cache\n"

except IOError:
print "File Doesn't Exists In Cache\n fetching file from server \n creating cache"
serv_proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_name = Req_path
print "HOST NAME:", host_name
try:
serv_proxy.connect((host_name, 80))
print 'Socket connected to port 80 of the host'
fileobj = serv_proxy.makefile('r', 0)
fileobj.write("GET " + "http://" + Req_path + " HTTP/1.0\n\n")

# Read the response into buffer
buffer = fileobj.readlines()

# Create a new file in the cache for the requested file.
# Also send the response in the buffer to client socket
# and the corresponding file in the cache
tmpFile = open("./" + Req_path, "wb")
for i in range(0, len(buffer)):
tmpFile.write(buffer[i])
Cli_Sock.send(buffer[i])
except:
print 'Illegal Request'

Cli_Sock.close()
while True:
# Start receiving data from the client
print 'Initiating server... \n Accepting connection\n'
Cli_Sock, addr = Serv_Sock.accept() # Accept a connection from client
#print addr

print ' connection received from: ', addr
message = Cli_Sock.recv(1024) #Recieves data from Socket

splitMessage = message.split()
if len(splitMessage) <= 1:
continue

caching_object(splitMessage, Cli_Sock)

最佳答案

你可以从 linux 的终端使用 httpie:

http [get/post] http://host:port/link_name/

在您的应用中,您可以使用请求:pip 安装请求

import requests

response = requests.get(url='url', proxies='proxies')
response.close()
print(response)

关于python - HTTP 代理服务器 [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30109605/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com