我有一个巨大的表,我需要处理其中的所有行.我总是得到这个丢失的连接消息,我无法重新连接并将光标恢复到它的最后位置.这基本上就是我在这里的代码:
#
import MySQLdb
class DB:
conn = None
def connect(self):
self.conn = MySQLdb.connect('hostname', 'user', '*****', 'some_table', cursorclass=MySQLdb.cursors.SSCursor)
def query(self, sql):
try:
cursor = self.conn.cursor()
cursor.execute(sql)
except (AttributeError, MySQLdb.OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql)
return cursor
#
#
db = DB()
sql = "SELECT bla FROM foo"
data = db.query(sql)
for row in data:
do_something(row)
#
Run Code Online (Sandbox Code Playgroud)
但我总是得到这个:
#
Traceback (most recent call last):
File "teste.py", line 124, in <module>
run()
File "teste.py", line 109, in run
for row in data: …Run Code Online (Sandbox Code Playgroud) 使用套接字获取网站时,我遇到了这种奇怪的行为.从下面的get_content()函数返回的字符串包括原始网站上不存在的一些"额外信息".
function get_content($a, $b, $c = "00")
{
$request = "arg01=" . $a;
$request .= "&arg02=" . $b;
$request .= "&arg03=" . $c;
$host = "www.site.com";
$script = "/page.php";
$method = "POST";
$request_length = strlen($request);
$header = "$method $script HTTP/1.1\r\n";
$header .= "Host: $host\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: $request_length\r\n";
$header .= "Connection: close\r\n\r\n";
$header .= "$request\r\n";
$socket = @fsockopen($host, 80, $errno, $errstr);
if ($socket) {
fputs($socket, $header);
while(!feof($socket)) {
$output .= fgets($socket);
}
fclose($socket);
}
return $output; …Run Code Online (Sandbox Code Playgroud)