227 lines
20 KiB
HTML
227 lines
20 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>diff</title>
|
|
<meta name="Generator" content="Vim/8.2">
|
|
<meta name="plugin-version" content="vim8.1_v2">
|
|
<meta name="settings" content="number_lines,whole_filler,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback">
|
|
<meta name="colorscheme" content="Guardian">
|
|
<style>
|
|
<!--
|
|
pre { font-family: monospace; color: #ffffff; background-color: #000000; }
|
|
body { font-family: monospace; color: #ffffff; background-color: #000000; }
|
|
* { font-size: 1em; }
|
|
.Statement { color: #00ffff; background-color: #000000; padding-bottom: 1px; }
|
|
.String { color: #00ffff; background-color: #000000; padding-bottom: 1px; }
|
|
.Conditional { color: #8080ff; background-color: #000000; padding-bottom: 1px; }
|
|
.LineNr { color: #c0c0c0; background-color: #000000; padding-bottom: 1px; }
|
|
.Function { color: #8080ff; background-color: #000000; padding-bottom: 1px; }
|
|
.Operator { color: #ffffff; background-color: #008080; padding-bottom: 1px; }
|
|
.Folded { color: #00ffff; background-color: #6c6c6c; padding-bottom: 1px; }
|
|
.Exception { color: #00ff00; background-color: #000000; padding-bottom: 1px; }
|
|
.Include { color: #00ff00; background-color: #000000; padding-bottom: 1px; }
|
|
.Repeat { color: #00ffff; background-color: #0000c0; padding-bottom: 1px; }
|
|
.Structure { color: #00ff00; background-color: #000000; padding-bottom: 1px; }
|
|
.Comment { color: #c0c0c0; background-color: #808080; padding-bottom: 1px; }
|
|
.Number { color: #ffffff; background-color: #c00000; padding-bottom: 1px; }
|
|
.DiffAdd { color: #ffffff; background-color: #c00000; padding-bottom: 1px; }
|
|
.DiffChange { color: #ffffff; background-color: #008000; padding-bottom: 1px; }
|
|
.DiffDelete { color: #ffffff; background-color: #0000c0; padding-bottom: 1px; }
|
|
.DiffText { color: #ffffff; background-color: #0000c0; padding-bottom: 1px; font-weight: bold; }
|
|
-->
|
|
<!--
|
|
table { table-layout: fixed; }
|
|
html, body, table, tbody { width: 100%; margin: 0; padding: 0; }
|
|
table, td, th { border: 1px solid; }
|
|
td { vertical-align: top; }
|
|
th, td { width: 50.0%; }
|
|
td div { overflow: auto; }
|
|
-->
|
|
</style>
|
|
<script>
|
|
<!--
|
|
|
|
/* function to open any folds containing a jumped-to line before jumping to it */
|
|
function JumpToLine()
|
|
{
|
|
var lineNum;
|
|
lineNum = window.location.hash;
|
|
lineNum = lineNum.substr(1); /* strip off '#' */
|
|
|
|
if (lineNum.indexOf('L') == -1) {
|
|
lineNum = 'L'+lineNum;
|
|
}
|
|
if (lineNum.indexOf('W') == -1) {
|
|
lineNum = 'W1'+lineNum;
|
|
}
|
|
var lineElem = document.getElementById(lineNum);
|
|
/* Always jump to new location even if the line was hidden inside a fold, or
|
|
* we corrected the raw number to a line ID.
|
|
*/
|
|
if (lineElem) {
|
|
lineElem.scrollIntoView(true);
|
|
}
|
|
return true;
|
|
}
|
|
if ('onhashchange' in window) {
|
|
window.onhashchange = JumpToLine;
|
|
}
|
|
|
|
-->
|
|
</script>
|
|
</head>
|
|
<body onload='JumpToLine();'>
|
|
<table id='vimCodeElement'>
|
|
<tr>
|
|
<th>server.py</th>
|
|
<th>formatted_with_black.server.py</th>
|
|
</tr><tr>
|
|
<td><div>
|
|
<pre>
|
|
<span id="W1L1" class="LineNr"> 1 </span><span class="Comment">#!/usr/bin/python3</span>
|
|
<span id="W1L2" class="LineNr"> 2 </span>
|
|
<span id="W1L3" class="LineNr"> 3 </span><span class="Include">import</span> socket
|
|
<span id="W1L4" class="LineNr"> 4 </span><span class="Include">import</span> threading
|
|
<span id="W1L5" class="LineNr"> 5 </span>
|
|
<span id="W1L6" class="LineNr"> 6 </span><span class="DiffChange">HOST = </span><span class="String DiffText">'</span><span class="String DiffText">0.0.0.0</span><span class="String DiffText">'</span><span class="DiffChange"> </span>
|
|
<span id="W1L7" class="LineNr"> 7 </span>PORT = <span class="Number">12345</span>
|
|
<span id="W1L8" class="LineNr"> 8 </span>
|
|
<span id="W1L9" class="LineNr"> 9 </span>server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
<span id="W1L10" class="LineNr">10 </span>server.bind((HOST, PORT))
|
|
<span id="W1L11" class="LineNr">11 </span>clients = []
|
|
<span id="W1L12" class="LineNr">12 </span>lock = threading.Lock()
|
|
<span id="W1L13" class="LineNr">13 </span>
|
|
<span id="W1L14" class="LineNr">14 </span>
|
|
<span id="W1L15" class="LineNr">15 </span><span class="Statement">def</span> <span class="Function">send_to_all</span>(conn, msg):
|
|
<span id="W1L16" class="LineNr">16 </span> <span class="Statement">with</span> lock:
|
|
<span id="W1L17" class="LineNr">17 </span> <span class="Repeat">for</span> client, _ <span class="Operator">in</span> clients:
|
|
<span id="W1L18" class="LineNr">18 </span> <span class="Conditional">if</span> client != conn:
|
|
<span id="W1L19" class="LineNr">19 </span> <span class="Exception">try</span>:
|
|
<span id="W1L20" class="LineNr">20 </span><span class="DiffChange"> client.sendall(msg.encode(</span><span class="String DiffText">'</span><span class="String DiffText">utf-8</span><span class="String DiffText">'</span><span class="DiffChange">)) </span>
|
|
<span id="W1L21" class="LineNr">21 </span> <span class="Exception">except</span> <span class="Structure">BrokenPipeError</span>:
|
|
<span id="W1L22" class="LineNr">22 </span> <span class="Function">print</span>(<span class="String">"</span><span class="String">Verbindung verloren.</span><span class="String">"</span>)
|
|
<span id="W1L23" class="LineNr">23 </span> <span class="Statement">with</span> lock:
|
|
<span id="W1L24" class="LineNr">24 </span> clients.remove((conn, nick))
|
|
<span id="W1L25" class="LineNr">25 </span> conn.close()
|
|
<span id="W1L26" class="LineNr">26 </span> <span class="Statement">break</span>
|
|
<span id="W1L27" class="LineNr">27 </span>
|
|
<span class="LineNr"> </span><span class="DiffDelete">----------------------------------------------------------------------------------------------------------------------------------------------------------</span>
|
|
<span id="W1L28" class="LineNr">28 </span><span class="Statement">def</span> <span class="Function">handle_client</span>(conn, addr):
|
|
<span id="W1L29" class="LineNr">29 </span><span class="DiffChange"> nick = conn.recv(</span><span class="Number DiffChange">1024</span><span class="DiffChange">).decode(</span><span class="String DiffText">'</span><span class="String DiffText">utf-8</span><span class="String DiffText">'</span><span class="DiffChange">) </span>
|
|
<span id="W1L30" class="LineNr">30 </span> <span class="Function">print</span>(f<span class="String">"</span><span class="String">Neuer Client verbunden: {addr} ({nick})</span><span class="String">"</span>)
|
|
<span id="W1L31" class="LineNr">31 </span> send_to_all(conn, f<span class="String">"</span><span class="String">{nick} hat den Chat betreten.</span><span class="String">"</span>)
|
|
<span id="W1L32" class="LineNr">32 </span>
|
|
<span id="W1L33" class="LineNr">33 </span> <span class="Statement">with</span> lock:
|
|
<span id="W1L34" class="LineNr">34 </span> clients.append((conn, nick))
|
|
<span id="W1L35" class="LineNr">35 </span>
|
|
<span id="W1L36" class="LineNr">36 </span> <span class="Repeat">while</span> <span class="Function">True</span>:
|
|
<span id="W1L37" class="LineNr">37 </span> <span class="Exception">try</span>:
|
|
<span id="W1L38" class="LineNr">38 </span><span class="DiffChange"> msg = conn.recv(</span><span class="Number DiffChange">1024</span><span class="DiffChange">).decode(</span><span class="String DiffText">'</span><span class="String DiffText">utf-8</span><span class="String DiffText">'</span><span class="DiffChange">) </span>
|
|
<span id="W1L39" class="LineNr">39 </span>
|
|
<span id="W1L40" class="LineNr">40 </span> <span class="Conditional">if</span> <span class="Operator">not</span> msg:
|
|
<span id="W1L41" class="LineNr">41 </span> <span class="Statement">break</span>
|
|
<span id="W1L42" class="LineNr">42 </span>
|
|
<span id="W1L43" class="LineNr">43 </span> <span class="Conditional">elif</span> msg == <span class="String">"</span><span class="String">exit</span><span class="String">"</span>:
|
|
<span id="W1L44" class="LineNr">44 </span> <span class="Function">print</span>(<span class="String">"</span><span class="String">Client getrennt:</span><span class="String">"</span>, addr)
|
|
<span id="W1L45" class="Folded">45 </span><span class="Folded">+-- 19 Zeilen: send_to_all(conn, f"{nick} hat den Chat verlassen")-------------------------------------------------------------------------------------------</span>
|
|
<span id="W1L64" class="LineNr">64 </span>
|
|
<span id="W1L65" class="LineNr">65 </span> <span class="Statement">break</span>
|
|
<span id="W1L66" class="LineNr">66 </span>
|
|
<span id="W1L67" class="LineNr">67 </span>
|
|
<span id="W1L68" class="LineNr">68 </span><span class="Statement">def</span> <span class="Function">start_server</span>():
|
|
<span id="W1L69" class="LineNr">69 </span> server.listen()
|
|
<span id="W1L70" class="LineNr">70 </span><span class="DiffChange"> </span><span class="Function DiffChange">print</span><span class="DiffChange">(f</span><span class="String DiffText">'</span><span class="String DiffText">Server läuft auf {HOST}:{PORT}</span><span class="String DiffText">'</span><span class="DiffChange">) </span>
|
|
<span id="W1L71" class="LineNr">71 </span>
|
|
<span id="W1L72" class="LineNr">72 </span> <span class="Repeat">while</span> <span class="Function">True</span>:
|
|
<span id="W1L73" class="LineNr">73 </span> conn, addr = server.accept()
|
|
<span id="W1L74" class="LineNr">74 </span> threading.Thread(target=handle_client, args=(conn, addr)).start()
|
|
<span id="W1L75" class="LineNr">75 </span>
|
|
<span id="W1L76" class="LineNr">76 </span>
|
|
<span id="W1L77" class="LineNr">77 </span><span class="Conditional DiffChange">if</span><span class="DiffChange"> __name__ == </span><span class="String DiffText">'</span><span class="String DiffText">__main__</span><span class="String DiffText">'</span><span class="DiffChange">: </span>
|
|
<span id="W1L78" class="LineNr">78 </span> <span class="Exception">try</span>:
|
|
<span id="W1L79" class="LineNr">79 </span> start_server()
|
|
<span id="W1L80" class="LineNr">80 </span> <span class="Exception">except</span> <span class="Structure">KeyboardInterrupt</span>:
|
|
<span id="W1L81" class="LineNr">81 </span> <span class="Function">print</span>(<span class="String">"</span><span class="String">Server wird heruntergefahren.</span><span class="String">"</span>)
|
|
<span id="W1L82" class="LineNr">82 </span>
|
|
<span id="W1L83" class="LineNr">83 </span> <span class="Statement">with</span> lock:
|
|
<span id="W1L84" class="Folded">84 </span><span class="Folded">+-- 5 Zeilen: for client, nick in clients:------------------------------------------------------------------------------------------------------------------</span>
|
|
</pre>
|
|
</div></td>
|
|
<td><div>
|
|
<pre>
|
|
<span id="W2L1" class="LineNr"> 1 </span><span class="Comment">#!/usr/bin/python3</span>
|
|
<span id="W2L2" class="LineNr"> 2 </span>
|
|
<span id="W2L3" class="LineNr"> 3 </span><span class="Include">import</span> socket
|
|
<span id="W2L4" class="LineNr"> 4 </span><span class="Include">import</span> threading
|
|
<span id="W2L5" class="LineNr"> 5 </span>
|
|
<span id="W2L6" class="LineNr"> 6 </span><span class="DiffChange">HOST = </span><span class="String DiffText">"</span><span class="String DiffText">0.0.0.0</span><span class="String DiffText">"</span><span class="DiffChange"> </span>
|
|
<span id="W2L7" class="LineNr"> 7 </span>PORT = <span class="Number">12345</span>
|
|
<span id="W2L8" class="LineNr"> 8 </span>
|
|
<span id="W2L9" class="LineNr"> 9 </span>server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
<span id="W2L10" class="LineNr">10 </span>server.bind((HOST, PORT))
|
|
<span id="W2L11" class="LineNr">11 </span>clients = []
|
|
<span id="W2L12" class="LineNr">12 </span>lock = threading.Lock()
|
|
<span id="W2L13" class="LineNr">13 </span>
|
|
<span id="W2L14" class="LineNr">14 </span>
|
|
<span id="W2L15" class="LineNr">15 </span><span class="Statement">def</span> <span class="Function">send_to_all</span>(conn, msg):
|
|
<span id="W2L16" class="LineNr">16 </span> <span class="Statement">with</span> lock:
|
|
<span id="W2L17" class="LineNr">17 </span> <span class="Repeat">for</span> client, _ <span class="Operator">in</span> clients:
|
|
<span id="W2L18" class="LineNr">18 </span> <span class="Conditional">if</span> client != conn:
|
|
<span id="W2L19" class="LineNr">19 </span> <span class="Exception">try</span>:
|
|
<span id="W2L20" class="LineNr">20 </span><span class="DiffChange"> client.sendall(msg.encode(</span><span class="String DiffText">"</span><span class="String DiffText">utf-8</span><span class="String DiffText">"</span><span class="DiffChange">)) </span>
|
|
<span id="W2L21" class="LineNr">21 </span> <span class="Exception">except</span> <span class="Structure">BrokenPipeError</span>:
|
|
<span id="W2L22" class="LineNr">22 </span> <span class="Function">print</span>(<span class="String">"</span><span class="String">Verbindung verloren.</span><span class="String">"</span>)
|
|
<span id="W2L23" class="LineNr">23 </span> <span class="Statement">with</span> lock:
|
|
<span id="W2L24" class="LineNr">24 </span> clients.remove((conn, nick))
|
|
<span id="W2L25" class="LineNr">25 </span> conn.close()
|
|
<span id="W2L26" class="LineNr">26 </span> <span class="Statement">break</span>
|
|
<span id="W2L27" class="LineNr">27 </span>
|
|
<span id="W2L28" class="LineNr">28 </span><span class="DiffAdd"> </span><span class="DiffAdd"> </span>
|
|
<span id="W2L29" class="LineNr">29 </span><span class="Statement">def</span> <span class="Function">handle_client</span>(conn, addr):
|
|
<span id="W2L30" class="LineNr">30 </span><span class="DiffChange"> nick = conn.recv(</span><span class="Number DiffChange">1024</span><span class="DiffChange">).decode(</span><span class="String DiffText">"</span><span class="String DiffText">utf-8</span><span class="String DiffText">"</span><span class="DiffChange">) </span>
|
|
<span id="W2L31" class="LineNr">31 </span> <span class="Function">print</span>(f<span class="String">"</span><span class="String">Neuer Client verbunden: {addr} ({nick})</span><span class="String">"</span>)
|
|
<span id="W2L32" class="LineNr">32 </span> send_to_all(conn, f<span class="String">"</span><span class="String">{nick} hat den Chat betreten.</span><span class="String">"</span>)
|
|
<span id="W2L33" class="LineNr">33 </span>
|
|
<span id="W2L34" class="LineNr">34 </span> <span class="Statement">with</span> lock:
|
|
<span id="W2L35" class="LineNr">35 </span> clients.append((conn, nick))
|
|
<span id="W2L36" class="LineNr">36 </span>
|
|
<span id="W2L37" class="LineNr">37 </span> <span class="Repeat">while</span> <span class="Function">True</span>:
|
|
<span id="W2L38" class="LineNr">38 </span> <span class="Exception">try</span>:
|
|
<span id="W2L39" class="LineNr">39 </span><span class="DiffChange"> msg = conn.recv(</span><span class="Number DiffChange">1024</span><span class="DiffChange">).decode(</span><span class="String DiffText">"</span><span class="String DiffText">utf-8</span><span class="String DiffText">"</span><span class="DiffChange">) </span>
|
|
<span id="W2L40" class="LineNr">40 </span>
|
|
<span id="W2L41" class="LineNr">41 </span> <span class="Conditional">if</span> <span class="Operator">not</span> msg:
|
|
<span id="W2L42" class="LineNr">42 </span> <span class="Statement">break</span>
|
|
<span id="W2L43" class="LineNr">43 </span>
|
|
<span id="W2L44" class="LineNr">44 </span> <span class="Conditional">elif</span> msg == <span class="String">"</span><span class="String">exit</span><span class="String">"</span>:
|
|
<span id="W2L45" class="LineNr">45 </span> <span class="Function">print</span>(<span class="String">"</span><span class="String">Client getrennt:</span><span class="String">"</span>, addr)
|
|
<span id="W2L46" class="Folded">46 </span><span class="Folded">+-- 19 Zeilen: send_to_all(conn, f"{nick} hat den Chat verlassen")-------------------------------------------------------------------------------------------</span>
|
|
<span id="W2L65" class="LineNr">65 </span>
|
|
<span id="W2L66" class="LineNr">66 </span> <span class="Statement">break</span>
|
|
<span id="W2L67" class="LineNr">67 </span>
|
|
<span id="W2L68" class="LineNr">68 </span>
|
|
<span id="W2L69" class="LineNr">69 </span><span class="Statement">def</span> <span class="Function">start_server</span>():
|
|
<span id="W2L70" class="LineNr">70 </span> server.listen()
|
|
<span id="W2L71" class="LineNr">71 </span><span class="DiffChange"> </span><span class="Function DiffChange">print</span><span class="DiffChange">(f</span><span class="String DiffText">"</span><span class="String DiffText">Server läuft auf {HOST}:{PORT}</span><span class="String DiffText">"</span><span class="DiffChange">) </span>
|
|
<span id="W2L72" class="LineNr">72 </span>
|
|
<span id="W2L73" class="LineNr">73 </span> <span class="Repeat">while</span> <span class="Function">True</span>:
|
|
<span id="W2L74" class="LineNr">74 </span> conn, addr = server.accept()
|
|
<span id="W2L75" class="LineNr">75 </span> threading.Thread(target=handle_client, args=(conn, addr)).start()
|
|
<span id="W2L76" class="LineNr">76 </span>
|
|
<span id="W2L77" class="LineNr">77 </span>
|
|
<span id="W2L78" class="LineNr">78 </span><span class="Conditional DiffChange">if</span><span class="DiffChange"> __name__ == </span><span class="String DiffText">"</span><span class="String DiffText">__main__</span><span class="String DiffText">"</span><span class="DiffChange">: </span>
|
|
<span id="W2L79" class="LineNr">79 </span> <span class="Exception">try</span>:
|
|
<span id="W2L80" class="LineNr">80 </span> start_server()
|
|
<span id="W2L81" class="LineNr">81 </span> <span class="Exception">except</span> <span class="Structure">KeyboardInterrupt</span>:
|
|
<span id="W2L82" class="LineNr">82 </span> <span class="Function">print</span>(<span class="String">"</span><span class="String">Server wird heruntergefahren.</span><span class="String">"</span>)
|
|
<span id="W2L83" class="LineNr">83 </span>
|
|
<span id="W2L84" class="LineNr">84 </span> <span class="Statement">with</span> lock:
|
|
<span id="W2L85" class="Folded">85 </span><span class="Folded">+-- 5 Zeilen: for client, nick in clients:------------------------------------------------------------------------------------------------------------------</span>
|
|
</pre>
|
|
</div></td>
|
|
</tr>
|
|
</table>
|
|
</body>
|
|
</html>
|
|
<!-- vim: set foldmethod=manual : -->
|
|
|