我在此python练习中遇到了一些麻烦

时间:2019-05-06 10:44:32

标签: python sockets

我对此练习有疑问,这是练习:

更改套接字程序,以便它计算数量 字符数,则它已显示3000个字符,但已接收并停止显示任何文本。该程序应检索整个文档并计算字符总数并显示计数 文件末尾的字符数。

原始代码:

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/romeo.txt 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(),end='')
mysock.close()

我的代码:

import socket
url=input("Enter a URL :\n")
count=0
host_name=url.split("/")[2]
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    mysock.connect((host_name, 80))
    cmd=('GET url HTTP/1.0\n\n'.encode())
    mysock.send(cmd)
except:
    print("Enter a valid URl")
exit()


while True:
    data = mysock.recv(512)
    count=count+len(data)
    if len(data) < 1:
        break
    print(data.decode())
mysock.close()

我的输出:

Enter a URL :
http://www.py4inf.com/code/romeo.txt
HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 06 May 2019 10:29:37 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>

我收到一条错误消息“ 400错误的请求”

有人可以帮我吗,谢谢

2 个答案:

答案 0 :(得分:0)

此代码对我有用:

import socket
from urllib.parse import urlsplit

url = urlsplit('http://www.py4inf.com/code/romeo.txt')
host = url.hostname
path = url.path

request = f'GET {path} HTTP/1.1\nHost: {host}\n\n'.encode('utf-8')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 80))
s.send(request)
result = s.recv(10000)
while (len(result) > 0):
    print(result.decode('utf-8'))

这是上面代码的输出:

 HTTP/1.1 200 OK
 Content-Type: text/plain
 Content-Length: 167
 Connection: keep-alive
 Keep-Alive: timeout=15
 Date: Thu, 09 May 2019 08:49:50 GMT
 Server: Apache
 Last-Modified: Fri, 04 Dec 2015 19:05:04 GMT
 ETag: "a7-526172f58b800"
 Accept-Ranges: bytes
 Cache-Control: max-age=604800, public
 Access-Control-Allow-Origin: *
 Access-Control-Allow-Headers: origin, x-requested-with, content-type
 Access-Control-Allow-Methods: GET

 But soft what light through yonder window breaks
 It is the east and Juliet is the sun
 Arise fair sun and kill the envious moon
 Who is already sick and pale with grief

编辑:这是已更正您的原始代码:

import socket
from urllib.parse import urlsplit

url = urlsplit(input("Enter a URL: ").strip())
count = 0
host = url.hostname
path = url.path
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    print(f'Connecting to {host} and fetching {path}')
    s.connect((host, 80))
    cmd = f'GET {path} HTTP/1.1\nHost: {host}\n\n'.encode('utf-8')
    s.send(cmd)
except:
    print("Enter a valid URl")
    exit()

while True:
    data = s.recv(512)
    count += len(data)
    if len(data) < 1:
        break
    print(data.decode())

s.close()

这是我使用上面的代码得到的输出:

Enter a URL: http://www.py4inf.com/code/romeo.txt
Connecting to www.py4inf.com and fetching /code/romeo.txt
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 167
Connection: keep-alive
Keep-Alive: timeout=15
Date: Thu, 09 May 2019 15:30:16 GMT
Server: Apache
Last-Modified: Fri, 04 Dec 2015 19:05:04 GMT
ETag: "a7-526172f58b800"
Accept-Ranges: bytes
Cache-Control: max-age=604800, public
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: origin, x-requested-with, content-type
Access-Control-Allow-Methods: GET

But soft what light through yonder window breaks
It is the east and Juliet 
is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

答案 1 :(得分:0)

试试这个,在 Python 3 上运行

engine = create_engine(os.getenv('MYSQL_DATABASE_URI')
values = []
for stock in stocks: #about 2000~3000 items
  sql = 'select * from ...'
  values.append(pd.read_sql_query(sql, engine))
return values