始终使用端口TCP

时间:2019-02-09 17:51:21

标签: python-3.x

我创建了一个代码,该代码允许我连接到html页面并在其中打印数据,但问题是每次执行代码时,我不得不手动更改此端口s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.listen(5) while True: conn, addr = s.accept() print('Got a connection from %s' % str(addr)) request = conn.recv(1024) request = str(request) print('Content = %s' % request) led_on = request.find('/?led=on') led_off = request.find('/?led=off') response ="" if led_on == 6: print('LED ON') led.value(1) if led_off == 6: i2c= I2C ( scl = Pin(5) , sda = Pin(4)) acc = mpu.accel(i2c) r = accelerometer.get_values() result.append(r) if len(result) > 8: result = result[1:] # print(str(r)) print('LED OFF') led.value(0) response = web_page() conn.send('HTTP/1.1 200 OK\n') conn.send('Content-Type: text/html\n') conn.send('Connection: close\n\n') conn.sendall(response) conn.close() ,因为在尝试停止后端口仍在运行的代码(使用中),因此我更改为另一个值以执行我的代码

-- Install configuration: ""

是否有防止这种想法?

1 个答案:

答案 0 :(得分:1)

  

是否有防止这种想法?

class SuperA<T extends InterfaceA> implements InterfaceA{} interface InterfaceA{} interface InterfaceB<T,T2 extends T>{ //T2 can only chain over T1 //but T1 can be anything, this case T1 extends Object, but it can extend any 1 Object + any multiple Interfaces <T3 extends T2> void foo2(T3 param); //T3 can chain or be like T and extend over something certain like T2 extends String&InterfaceA //void foo3(InterfaceB<? super InterfaceA,InterfaceA> example); //the only time you are allowed to use super is inside methods and only ? can use them(one the left, ie ? super T is allowed but T super ? not), but again this is only chaining } class DerivedA<T2 extends SuperA&InterfaceA> extends SuperA implements InterfaceB<SuperA,T2>{ //here you are making sure that T2 is always parent+InterfaceA and in children DerivedAChild extends DerivedA<T2 extends DerivedA&InterfaceA> is always this+InterfaceA @Override public <T3 extends T2> void foo2(T3 param) { } } 之前,您需要设置以下选项:

.bind()

它放宽了内核TCP层执行的一些计时强制,在开发和调试过程中您会发现这很方便。一旦代码稳定并在生产中发布,它重新启动的频率就会减少,因此时间问题就更少了。

使用当前代码,在CTRL / C之后,您应该会在一段时间内看到端口80套接字处于TIME_WAIT状态。在计时器到期之前,任何应用程序都无法监听80。设置套接字选项后,套接字将被快速回收以允许新的应用程序进程监听80。

Warren Young很友善地在What is the meaning of SO_REUSEADDR (setsockopt option) - Linux?

中解释了此TCP设计选择的动机。