将 python 服务器连接到互联网
Connect python server to internet
我正在尝试制作一个 python 服务器,并通过浏览器从世界任何地方使用任何互联网连接访问它。目前我有以下来自 youtube 教程的代码:
import socket
from _thread import *
host=''
port=5555
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.bind((host,port))
except socket.error as e:
print(str(e))
s.listen(5)
print("Waiting for a connection")
def threadedClient(conn):
conn.send(str.encode('Welcome, type your info\n'))
storeStr = 'Server Output:'
while True:
data = conn.recv(2048)
reply = data.decode('utf-8')
if not data:
break
for x in reply:
if x=='\n':
conn.sendall(str.encode(storeStr))
storeStr = 'Server Output:'
else:
storeStr=storeStr+reply
print(storeStr)
conn.close()
while True:
conn, addr = s.accept()
print('connected to:'+addr[0]+':'+str(addr[1]))
start_new_thread(threadedClient,(conn,))
使用此代码,我能够通过命令提示符连接到服务器并与之交互。但是,尝试通过浏览器连接会导致:
GET / HTTP/1.1
Host: 192.168.1.56:5555
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/67.0.3396.87 Safari/537.36
Accept:
text/html,application/xhtml+xml,
application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,en-GB;q=0.8
Unhandled exception in thread started by <function threadedClient at
0x0000027B86E0C2F0>
我曾尝试将发送的字符串更改为更像 HTTP 的字符串,例如 "POST / HTTP/1.1" 或 "Test",但这并没有改变任何内容。
此外,我已经在路由器上为笔记本电脑设置了端口转发,想知道是否需要采取进一步的措施才能使 WAN 正常工作。谢谢!
Telnet,用于连接到 Python 服务器,是一个(相当基本的)面向文本的协议。本质上,它发送 ASCII 数据。
您的 Web 浏览器使用的 HTTP 是一种超文本协议。因此,它可能会发送您的服务器无法解释(或误解)的数据。如果您想从 Web 浏览器访问您的服务器,您将需要实施 http
协议(或至少它的一个子集)。
好的,我现在有一个 HTTP 文件服务器,同样来自 youtube 教程。下面是代码:
from http.server import HTTPServer, BaseHTTPRequestHandler
class Serv(BaseHTTPRequestHandler):
def do_GET(self):
if self.path=='/':
self.path='/index.html'
try:
file_to_open=open(self.path[1:]).read()
self.send_response(200)
except:
file_to_open='file not found'
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(file_to_open,'utf-8'))
httpd=HTTPServer(('192.168.1.56',8081),Serv)
httpd.serve_forever()
我假设缺少套接字库是因为套接字由 HTTPServer 库处理。但我仍然不确定如何将其连接到 WAN。我已经在我的路由器上完成了必要的端口转发配置,但是当我检查 yougetsignal.com 时端口仍然关闭。对于端口转发,我使用 TCP 将 IP 192.168.1.56 转发到端口 8081。我为笔记本电脑手动分配了一个 IP,该 IP 不应与网络上的任何其他设备重叠,因为 IP 最初是通过 DHCP 获得的。我还需要对代码做些什么吗?或者也许有一种方法可以使用带有 HTTPServer 的套接字来实现结果,因为 http.server 库似乎没有太多文档...
我正在尝试制作一个 python 服务器,并通过浏览器从世界任何地方使用任何互联网连接访问它。目前我有以下来自 youtube 教程的代码:
import socket
from _thread import *
host=''
port=5555
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.bind((host,port))
except socket.error as e:
print(str(e))
s.listen(5)
print("Waiting for a connection")
def threadedClient(conn):
conn.send(str.encode('Welcome, type your info\n'))
storeStr = 'Server Output:'
while True:
data = conn.recv(2048)
reply = data.decode('utf-8')
if not data:
break
for x in reply:
if x=='\n':
conn.sendall(str.encode(storeStr))
storeStr = 'Server Output:'
else:
storeStr=storeStr+reply
print(storeStr)
conn.close()
while True:
conn, addr = s.accept()
print('connected to:'+addr[0]+':'+str(addr[1]))
start_new_thread(threadedClient,(conn,))
使用此代码,我能够通过命令提示符连接到服务器并与之交互。但是,尝试通过浏览器连接会导致:
GET / HTTP/1.1
Host: 192.168.1.56:5555
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/67.0.3396.87 Safari/537.36
Accept:
text/html,application/xhtml+xml,
application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,en-GB;q=0.8
Unhandled exception in thread started by <function threadedClient at
0x0000027B86E0C2F0>
我曾尝试将发送的字符串更改为更像 HTTP 的字符串,例如 "POST / HTTP/1.1" 或 "Test",但这并没有改变任何内容。
此外,我已经在路由器上为笔记本电脑设置了端口转发,想知道是否需要采取进一步的措施才能使 WAN 正常工作。谢谢!
Telnet,用于连接到 Python 服务器,是一个(相当基本的)面向文本的协议。本质上,它发送 ASCII 数据。
您的 Web 浏览器使用的HTTP 是一种超文本协议。因此,它可能会发送您的服务器无法解释(或误解)的数据。如果您想从 Web 浏览器访问您的服务器,您将需要实施 http
协议(或至少它的一个子集)。
好的,我现在有一个 HTTP 文件服务器,同样来自 youtube 教程。下面是代码:
from http.server import HTTPServer, BaseHTTPRequestHandler
class Serv(BaseHTTPRequestHandler):
def do_GET(self):
if self.path=='/':
self.path='/index.html'
try:
file_to_open=open(self.path[1:]).read()
self.send_response(200)
except:
file_to_open='file not found'
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(file_to_open,'utf-8'))
httpd=HTTPServer(('192.168.1.56',8081),Serv)
httpd.serve_forever()
我假设缺少套接字库是因为套接字由 HTTPServer 库处理。但我仍然不确定如何将其连接到 WAN。我已经在我的路由器上完成了必要的端口转发配置,但是当我检查 yougetsignal.com 时端口仍然关闭。对于端口转发,我使用 TCP 将 IP 192.168.1.56 转发到端口 8081。我为笔记本电脑手动分配了一个 IP,该 IP 不应与网络上的任何其他设备重叠,因为 IP 最初是通过 DHCP 获得的。我还需要对代码做些什么吗?或者也许有一种方法可以使用带有 HTTPServer 的套接字来实现结果,因为 http.server 库似乎没有太多文档...