python + worlds_simplist_web_browser + socket.error:[Errno 128]传输端点未连接

时间:2017-01-10 20:59:40

标签: python-2.7

我正在关注此课程here和本书here。 我无法让worlds_simplist_web_browser工作,chapt12第144页。 任何人都可以告诉我的代码有什么问题。我目前正在socket.error: [Errno 128] Transport endpoint is not connected。或者这是一个网络问题?我现在在工作。我可以得到卷曲和工作,所以不知道还有什么要检查。

$ python --version
Python 2.7.5

$ cat worlds_simplist_web_browser.py
import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')

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

mysock.close()


$ python worlds_simplist_web_browser.py
Traceback (most recent call last):
  File "worlds_simplist_web_browser.py", line 5, in <module>
    mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')
socket.error: [Errno 128] Transport endpoint is not connected


$ ls
test  worlds_simplist_web_browser.py


$ wget http://www.py4inf.com/code/romeo.txt
--2017-01-11 09:51:37--  http://www.py4inf.com/code/romeo.txt
Resolving localhost (localhost)... 127.0.0.1, ::1
Connecting to localhost (localhost)|127.0.0.1|:53128... connected.
Proxy request sent, awaiting response... 200 OK
Length: 167 [text/plain]
Saving to: ‘romeo.txt’

100%[======================================>] 167         --.-K/s   in 0s

2017-01-11 09:51:37 (5.87 MB/s) - ‘romeo.txt’ saved [167/167]



$ curl http://www.py4inf.com/code/romeo.txt
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

EDIT1

我在不同的环境中尝试过这种方法,但无法让它发挥作用。 我可以得到卷曲和工作。 我接下来就是在家里试试。

root@e072d2cc7f74:/usr/share/nginx/html/python/chapt12# python worlds_simplist_web_browser.py
Traceback (most recent call last):
  File "worlds_simplist_web_browser.py", line 4, in <module>
    mysock.connect(('www.py4inf.com', 80))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 110] Connection timed out

root@e072d2cc7f74:/usr/share/nginx/html/python/chapt12# sudo python worlds_simplist_web_browser.py
Traceback (most recent call last):
  File "worlds_simplist_web_browser.py", line 4, in <module>
    mysock.connect(('www.py4inf.com', 80))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 110] Connection timed out


root@e072d2cc7f74:/usr/share/nginx/html/python/chapt12# sudo -i
root@e072d2cc7f74:~#
root@e072d2cc7f74:~# python /usr/share/nginx/html/python/chapt12/worlds_simplist_web_browser.py
Traceback (most recent call last):
  File "/usr/share/nginx/html/python/chapt12/worlds_simplist_web_browser.py", line 4, in <module>
    mysock.connect(('www.py4inf.com', 80))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 110] Connection timed out
root@e072d2cc7f74:~#

EDIT2

认为它可能是防火墙工作,不确定。 无论如何通过在防火墙内的服务器上托管我的文件romeo.txt来使它工作,所以我不必通过防火墙。

$ cat local_worlds_simplist_web_browser.py
import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('172.21.200.37', 80))
mysock.send('GET http://172.21.200.37/~dir_name/romeo.txt HTTP/1.0\n\n')  # works
#mysock.send("GET /~dir_name/romeo.txt HTTP/1.0\n Host:http://172.21.200.37/\n\n") # works also other format to above

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

mysock.close()

$ python local_worlds_simplist_web_browser.py
HTTP/1.1 200 OK
Date: Wed, 11 Jan 2017 20:16:17 GMT
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Wed, 11 Jan 2017 20:03:20 GMT
ETag: "3215b4-a7-545d717aa5cf7"
Accept-Ranges: bytes
Content-Length: 167
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Connection: close
Content-Type: text/plain

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 :(得分:0)

主机名不会进入GET请求 - 它实际上是一个单独的标题字段。尝试:

ref?.queryOrdered(byChild: "project").queryEqual(toValue: "inbox").observe(.value, with: { (snapshot:FIRDataSnapshot) in var newTasks = [Task]() for sweet in snapshot.children { let sweetObject = Task(snapshot: sweet as! FIRDataSnapshot) newSweets.append(sweetObject) } self.tasks = newTasks self.tableView.reloadData() }) { (error:Error) -> Void in print(error.localizedDescription) }

(另请注意,标题中的换行符是正确的CRLF,而不仅仅是LF,但我认为你通常可以逃脱它。)