Socket.error [错误 10060] [英] Socket.error [Error 10060]

查看:104
本文介绍了Socket.error [错误 10060]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我试图运行的代码.但是程序产生了一个socket.error.我有一个端口为 8080 的网络代理将我连接到 Internet,我还需要在此处添加哪些详细信息才能创建此套接字连接?

This is the code I am trying to run. But the program produces a socket.error. I have a network proxy with port 8080 which connects me to the Internet, what more details do I have to add here to create this socket connection?

import socket

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

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

mysock.close()

推荐答案

我在运行您的代码时也遇到了类型错误,并且在连接套接字时没有出现错误.使用套接字库时,请使用 makefile 方法,这样您就不必处理烦人的细节.

I too got a type error upon running your code, and did not have an error connecting the socket. When using the socket library, make use of the makefile method so you won't have to deal with annoying details.

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
my_sock_input = mysock.makefile('r')
my_sock_output = mysock.makefile('w')

现在 my_sock_input 可以使用诸如 readline() 之类的方法,而无需保留任何字节的详细信息.输出同样方便,但有写.记得全部关闭!

Now my_sock_input can use methods like readline(), without any details on bytes to reserve or wotnot. Same convenience stuff for output, but with write. Remember to close all of them!

至于您的问题,我尝试使用我的 makefile 变量编写类似的内容,但没有收到任何消息.所以还有一些其他问题.

As to your problem, I tried writing similar things using my makefile variables and I wasn't recieving any message back. So there is some other issue there.

现在,解决方案.下载 url 并读取其内容的一种更简单的方法是使用 urllib.request 库.如果您使用的是 Python 2.7,只需 import urrlib.

Now, the solution. A simpler way to download a url and read its contents is using the urllib.request library. If you are on Python 2.7, just import urrlib.

import urllib.request

data =  urllib.request.urlopen('http://www.pythonlearn.com/code/intro-short.txt')
readable = data.read()
print(readable)

这篇关于Socket.error [错误 10060]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆