安装必要的库: 使用 pip install requests 安装 requests 库,用于发送 HTTP 请求。 定义功能模块: create_forkwall() 函数定义翻墙软件的主功能模块。 用户界面设计: 使用 input() 函数让用户选择是否连接到互联网。 根据用户选择,调用相应的函数来获取IP地址和服务器端口。 处理网络请求: 使用 requests.get() 发送 HTTP 请求获取服务器响应。 使用 raise_for_status() 提交请求并处理状态码。 处理响应: 从请求结果中获取IP地址、服务器端口和域名。 显示获取到的信息给用户。 处理错误情况: 使用 try-except 坚持处理网络连接和请求失败,提示用户。 代码实现: import requests import os def create_forkwall(): print("Welcome to Forkwall Browser!") print("1. Choose to connect to the internet") print("2. Choose to manually select an IP address from the list below") choice = input("Choose your option (1 or 2): ") if choice == '1': # 连接到互联网 ip = input("Enter your IP address: ") url = f"http://{server_info['IP地址']}://88".format(**server_info) response = requests.get(url, headers={'User-Agent': ...
-
安装必要的库:
- 使用
pip install requests安装requests库,用于发送 HTTP 请求。
- 使用
-
定义功能模块:
create_forkwall()函数定义翻墙软件的主功能模块。
-
用户界面设计:
- 使用
input()函数让用户选择是否连接到互联网。 - 根据用户选择,调用相应的函数来获取IP地址和服务器端口。
- 使用
-
处理网络请求:
- 使用
requests.get()发送 HTTP 请求获取服务器响应。 - 使用
raise_for_status()提交请求并处理状态码。
- 使用
-
处理响应:
- 从请求结果中获取IP地址、服务器端口和域名。
- 显示获取到的信息给用户。
-
处理错误情况:
- 使用
try-except坚持处理网络连接和请求失败,提示用户。
- 使用
代码实现:
import requests
import os
def create_forkwall():
print("Welcome to Forkwall Browser!")
print("1. Choose to connect to the internet")
print("2. Choose to manually select an IP address from the list below")
choice = input("Choose your option (1 or 2): ")
if choice == '1':
# 连接到互联网
ip = input("Enter your IP address: ")
url = f"http://{server_info['IP地址']}://88".format(**server_info)
response = requests.get(url, headers={'User-Agent': 'Mozilla/5. (Windows NT 1.; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/12... Safari/537.36'})
response.raise_for_status()
data = response.json()
print(f"Connected to the internet.")
print(f"IP address: {data['IP地址']}")
print(f"Server端口: {data['Server端口']}")
print(f"域名: {data['域名']}")
elif choice == '2':
# 不连接到互联网,手动选择IP address
ip = input("Enter an IP address to connect to: ")
url = f"http://{server_info['IP地址']}://88".format(**server_info)
response = requests.get(url, headers={'User-Agent': 'Mozilla/5. (Windows NT 1.; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/12... Safari/537.36'})
response.raise_for_status()
data = response.json()
print(f"Connected to the internet.")
print(f"IP address: {data['IP地址']}")
print(f"Server端口: {data['Server端口']}")
print(f"域名: {data['域名']}")
return
def server_info():
return {
'IP地址': 'localhost',
'Server端口': 88,
'域名': 'example.com'
}
if __name__ == "__main__":
server_info = server_info()
create_forkwall()
代码解释:
server_info()函数:定义了一个简单配置的服务器信息,包括IP地址、服务器端口和域名。create_forkwall()函数:处理用户的选择,根据选择连接或不连接,执行相应的操作。- 连接到互联网:输入IP地址,请求服务器响应,获取IP、服务器端口和域名。
- 不连接到互联网:输入IP地址,登录到服务器,获取响应信息。
- 用户界面:通过交互式输入引导用户选择连接或不连接,显示相应的响应信息。
该代码提供了一个基础的翻墙功能,用户可以根据需求扩展功能,如添加更多服务器或增加连接选项。

相关文章







